Home > database >  I keep getting Null check operator used on a null value error
I keep getting Null check operator used on a null value error

Time:06-17

I keep getting null check operator used on a null value within these lines of code below and I can't seem to fathom why. I have referenced quite a number of materials on this platform but none of them seems to be helping. Assist if you can please?

AuthServices authServices = AuthServices();

void checkRegistration() {
final isValid = registerKey.currentState!.validate();
if (!isValid) {
  return;
}
registerKey.currentState!.save();

authServices.auth
    .createUserWithEmailAndPassword(
  email: registerEmailController.text.trim(),
  password: registerPasswordController.text.trim(),
)
    .then((value) {
  FirebaseFirestore.instance.collection("users").doc(value.user!.uid).set({
    "uid": value.user!.uid,
    "email": value.user!.email,
    "name": value.user!.displayName,
    "phone": value.user!.phoneNumber,
    "time": Timestamp.now(),
  });
});

}

CodePudding user response:

Add a condition to check if the user is null before saving it to the document. I think that's where it's being null and checks other value

AuthServices authServices = AuthServices();

void checkRegistration() {
if(registerKey.currentState != null)
{
    final isValid = registerKey.currentState!.validate();
    if (!isValid) {
      return;
    }

    registerKey.currentState!.save();
}
authServices.auth
    .createUserWithEmailAndPassword(
  email: registerEmailController.text.trim(),
  password: registerPasswordController.text.trim(),
)
    .then((value) {
if(value.user != null)// check if the user is recived in value before assigning 
{
  FirebaseFirestore.instance.collection("users").doc(value.user!.uid).set({
    "uid": value.user!.uid,
    "email": value.user!.email,
    "name": value.user!.displayName,
    "phone": value.user!.phoneNumber,
    "time": Timestamp.now(),
  });
});
}
}
  • Related