firebaseflutterfirebase-authentication

User displayName returns null in Firebase Auth


I want to add a user's DisplayName upon creating a new user. When I tried to use updateProfile method, it gives below warning

/// Updates a user's profile data.

  @Deprecated(
    'Will be removed in version 2.0.0. '
    'Use updatePhotoURL and updateDisplayName instead.',

How can get around this ??

Future<AppUser> createUserWithEmailAndPassword(
      String email, String password) async {
    final UserCredential userCredential =
        await _firebaseAuth.createUserWithEmailAndPassword(
            email: email.trim(), password: password.trim());
    await userCredential.user.updateDisplayName('Mohamed Rahuman');
    User user = userCredential.user;
    print(user);
    return _userFromFirebaseUser(user);
  }

Logs

flutter: User(displayName: null, email: test1@gmail.com, emailVerified: false, isAnonymous: false, metadata: UserMetadata(creationTime: 2021-06-06 10:35:13.689, lastSignInTime: 2021-06-06 10:35:13.689), phoneNumber: null, photoURL: null, providerData, [UserInfo(displayName: null, email: test1@gmail.com, phoneNumber: null, photoURL: null, providerId: password, uid: test1@gmail.com)], refreshToken: , tenantId: null, uid: gghhhhhhhh55555666)

THIS IS FIXED: with firebase_auth 1.3.0 (see the Changelog)


Solution

  • Had the same issue. Solved by calling:

    await user.reload();
    user = await _auth.currentUser;
    

    this is the full code snippet:

    UserCredential result = await _auth.createUserWithEmailAndPassword(
              email: email, password: password);
    User? user = result.user;
    if (user != null) {
       //add display name for just created user
       user.updateDisplayName(displayName);
       //get updated user
       await user.reload();
       user = await _auth.currentUser;
       //print final version to console
       print("Registered user:");
       print(user);
    }