Home > database >  how to solve flutter formstate method?
how to solve flutter formstate method?

Time:01-04

I have this class in flutter



abstract class ForgetPasswordController extends GetxController {
  checkemail(); 
}

class ForgetPasswordControllerImp extends ForgetPasswordController {
  
  CheckEmailData checkEmailData  = CheckEmailData(Get.find()) ; 

  GlobalKey<FormState> formstate = GlobalKey<FormState>();
  
  StatusRequest? statusRequest = StatusRequest.none ;
  

   late TextEditingController email;

  @override
  checkemail() async  {
    if (formstate.currentState!.validate()){
       statusRequest = StatusRequest.loading; 
      update() ; 
      var response = await checkEmailData.postdata(email.text);
      print("=============================== Controller $response ");
      statusRequest = handlingData(response);
      if (StatusRequest.success == statusRequest) {
        if (response['status'] == "success") {
          // data.addAll(response['data']);
          Get.offNamed(AppRoute.verfiyCode , arguments: {
            "email" : email.text
          });
        } else {
          Get.defaultDialog(title: "ُWarning" , middleText: "Email Not Found"); 
          statusRequest = StatusRequest.failure;
        }
      }
      update();
    }
  }

 
  @override
  void onInit() {
    
    email = TextEditingController();
    super.onInit();
  }

  @override
  void dispose() {
    email.dispose();
    super.dispose();
  }
}


but when try this method on emulator i have error message in console says : E/flutter ( 7876): [ERROR:flutter/runtime/dart_vm_initializer.cc(41)] Unhandled Exception: Null check operator used on a null value E/flutter ( 7876): #0 ForgetPasswordControllerImp.checkemail package:powerecommerce/…/forgetpassword/forgetpassword_controller.dart:27 E/flutter ( 7876): #1 ForgetPassword.build..

Im just a beginner and idk how i can solve this problem

I tried to see if there is any problem with php api files but all files are correct! and i researched for all the basic problem solving and the same problem exist

CodePudding user response:

Based on the error message, I guess it is coming while using !. here

 checkemail() async  {
    if (formstate.currentState!.validate()){
       statusRequest = StatusRequest.loading; 

You can do null check or just provide default value as false on null case like

 checkemail() async  {
    final isValided  = formstate.currentState?.validate()??false;
    if (isValided){
       statusRequest = StatusRequest.loading; 

CodePudding user response:

In my opinion, your email is not initialized before using it.

Instead late TextEditingController email;,

try changing it to TextEditingController email = TextEditingController();

CodePudding user response:

i have a problem with this code

abstract class VerifyCodeController extends GetxController {
  checkCode();
  goToResetPassword(String verifycode);
}

class VerifyCodeControllerImp extends VerifyCodeController {
  String? email;

  VerifyCodeForgetPasswordData verifyCodeForgetPasswordData =
      VerifyCodeForgetPasswordData(Get.find());

  StatusRequest statusRequest = StatusRequest.none;

  @override
  checkCode() {}

  @override
  goToResetPassword(verifycode) async {
    statusRequest = StatusRequest.loading;
    update();
    var response =
        await verifyCodeForgetPasswordData.postData(email!, verifycode);
    statusRequest = handlingData(response);
    if (StatusRequest.success == statusRequest) {
      if (response['status'] == "success") {
        Get.offNamed(AppRoute.resetPassword , arguments: {
          "email" : email
        });
      } else {
        Get.defaultDialog(
            title: "ُWarning", middleText: "Verify Code Not Correct");
        statusRequest = StatusRequest.failure;
      }
    }
    update();
  }

  @override
  void onInit() {
    email = Get.arguments['email'];
    super.onInit();
  }
}

when i try it the console type this message : D/ViewRootImpl@844864dMainActivity: ViewPostIme pointer 0 D/ViewRootImpl@844864dMainActivity: ViewPostIme pointer 1 E/flutter ( 7876): [ERROR:flutter/runtime/dart_vm_initializer.cc(41)] Unhandled Exception: Null check operator used on a null value

i don't have can i solve it

  • Related