2
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.

Md. Yeasin Sheikh
  • 54,221
  • 7
  • 29
  • 56
Nirosh
  • 63
  • 8

1 Answers1

4

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

Kaushik Chandru
  • 15,510
  • 2
  • 12
  • 30
  • Using the formal terms would be much better for further researches. Would you mind including the proper term "Null-aware operators" and maybe this link: ’https://dart.dev/codelabs/dart-cheatsheet#null-aware-operators’ – KHAN Aug 26 '22 at 05:16
  • Yeah but tried to explain in simple terms, so its easily understandable.. Thank you for mentioning and adding the link. I missed it. my apologies :) – Kaushik Chandru Aug 26 '22 at 05:21