Saad Ebad
Saad Ebad

Reputation: 226

Null safety in dart (Null check operator used on a null value)

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.

Upvotes: 0

Views: 1034

Answers (1)

reza
reza

Reputation: 1508

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

Upvotes: 1

Related Questions