skip to Main Content

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


  1. The reason you got the error is Text widget only accept String which can not be null. But displayName or email in your code is null.

    Try this:

    accountName: Text(auth.currentUser?.displayName ?? '')
    accountEmail: Text(auth.currentUser?.email ?? '')
    
    Login or Signup to reply.
  2. UserAccountsDrawerHeader(
                  accountName: Text(auth.currentUser!.displayName),
                  accountEmail: Text(auth.currentUser!.email),
                ),
    

    I would assume that this callable Text expects values of type String not String? for accountName and accountEmail. 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:

    UserAccountsDrawerHeader(
      accountName: Text(auth.currentUser?.displayName ?? 'No display name'),
      accountEmail: Text(auth.currentUser?.email ?? 'No email'),
    ),
    

    The ?.displayName tries to find the displayName attribute under currentUser and if it is not found it returns a null value and ?? returns the value after it if the value before it is null.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search