Home > Enterprise >  What does "!" this operator means in dart when it using like below?
What does "!" this operator means in dart when it using like below?

Time:08-26

final user = FirebaseAuth.instance.currentUser;
                print(user);
                if (user!.emailVerified) {
                  print('User is veryfied');
                } else {
                  print('please verify');
                }

Without it I get an error that says Property emailVerified cannot be accessed on User? because it is potentially null

but when I add ? this to avoid it

I get this error A value of type 'bool?' can't be assigned to a variable of type bool because 'bool?' is nullable and 'bool' isn't.

CodePudding user response:

When you add a ? it means that it is can have a value or null. But if you add ! it means that the value cannot be null but we are unsure of the value..

Now for example if you are sure that a user is available then you can add

user!.emailVerified;

if for example you are unsure then you can add

user?.emailVerified

Now if user is null in the second case then it cannot be used in a condition because null is not a condition like true or false. So you may have to supply a default too

(user?.emailVerified ?? false)

This means that if the previous value is null then it will use false

  • Related