Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading