I want to display the displayName and email from FirebaseAuth on an app screen but when I try to put them into Text() widgets I get an error.
Here is the error:
The argument type ‘String?’ can’t be assigned to the parameter type ‘String’.dartargument_type_not_assignable
User? get currentUser
Here is the code where I am trying to do this:
UserAccountsDrawerHeader(
accountName: Text(auth.currentUser!.displayName),
accountEmail: Text(auth.currentUser!.email),
),
I have tried a few different things but nothing gets rid of the error.
How do I get this to work?
Thanks
2
Answers
The reason you got the error is
Text
widget only acceptString
which can not be null. ButdisplayName
oremail
in your code is null.Try this:
I would assume that this callable
Text
expects values of typeString
notString?
foraccountName
andaccountEmail
. The difference here is the question mark?
at the end of the type name which indicates that is nullable.https://dart.dev/null-safety
Nullable means that the value accepts a null value but Text expects non-nullable and you are passing values of type
String?
to it which is causing this compiler error. Check if this works:The
?.displayName
tries to find thedisplayName
attribute undercurrentUser
and if it is not found it returns a null value and??
returns the value after it if the value before it is null.