Home > Mobile >  The bool variable isn't updating?
The bool variable isn't updating?

Time:07-05

The error isn't getting set to true, why so. Even after using setState.

class _OtpState extends State<Otp> {
  bool error = false;

  OTPApi(String phoneNumber, String otp) async {
    
    if (user.StatusCode == 0) {
      KEY = user.Data!["CustomerKey"];
      await prefs.setString('KEY', KEY!);
    } else {
      setState(() {
        error = true;
      });
    }
  }

API function. ERROR: The following _CastError was thrown while calling onChanged: Null check operator used on a null value

OTPApi(PhoneNumber!, pin).then((value) => {
                    showDialog(
                        context: context,
                        builder: (context) {
                          return error
                              ? const AlertDialog(
                                  title: Text("Verification Code"),
                                  content: Text('wrong otp'),
                                )
                              : const Loading();
                        })
                  });
            },

CodePudding user response:

The error is not set to true. Because the program isn't executing the function. Because the part to focus here is the error , Null check operator used on a null value.

The meaning of the error is ! operator is used on Null value

This error is shown because the phoneNumber is null for some reason. Because of which the OTPApi function is not executing and it throws exception and comes out of execution.

Check the phoneNumber is not null before calling the function.This should fix your problem

Or

Change OTPApi(String phoneNumber, String otp) to OTPApi(String? phoneNumber, String otp)

Hope it helps!!

  • Related