0

Hello i know it is very common error in flutter when we deal with dart null safety. I have come to know why this error is occuring but i am unable to fix this error."Null check operator used on a null value" this error appears when we use bang(!) operator with the nullable variable but i don't know how to fix this. As in my code i am getting this issue in this line where i am getting the email of a user in firebase

String? get user => _firebaseUser.value!.email;

and i am using this value in other dart file where i am checking that if user is null then go to the login page but if user is not null than i want to keep user signed in.

@override
  Widget build(BuildContext context) {
    return Obx((){
      return Get.find<FirebaseController>().user!=null ? MainPage() : LoginPage();
    });
  }

how to fix this error.

Saad Ebad
  • 206
  • 5
  • 16

1 Answers1

1

use this instead:

String? get user => _firebaseUser.value?.email;

your value may be null so you can't use ! for it. the above code returns null if the _firebaseUser.value is null, otherwise returns _firebaseUser.value.email

reza
  • 1,354
  • 1
  • 7
  • 25
  • i also did this but when i login then email is not reteriving and i am getting message that null value is returning instead of email – Saad Ebad Jan 23 '22 at 05:51
  • probably there is a problem with your firebase configuration. but the correct approach to use email is this. – reza Jan 23 '22 at 05:53
  • actually i was doing a silly mistake i.e i was not assigning controllers in textfformfields that's why i was getting getting null – Saad Ebad Jan 23 '22 at 07:53