User displayName returns null in Firebase Auth

Viewed 2303

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 ??

      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);
  }

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)

3 Answers

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);
}

I had the same issue in my signup function while updating (updatePhotoURL) You need to update your user by

ourUser = FirebaseAuth.instance.currentUser;
await ourUser!.updatePhotoURL(defaultProfileImage);
await ourUser!.displayName(userName);
Check how I did it
  Future register(
  String email,
  String password,
  String? name,
  String? mob,
  String? description,
  //DateTime? signUPDate,
      ) async {
    setIsLoading(true);
    try {
      UserCredential authResult = await _firebaseAuth
          .createUserWithEmailAndPassword(email: email, password: password,);
      ourUser = authResult.user;
      setIsLoading(false);
      //create new doc for the user with his unique uid
      await UserDatabaseServices(uid: ourUser!.uid)
          .addUserData(ourUser!.uid,email,password,name,mob,description,currentTime);
      ourUser = _firebaseAuth.currentUser;
      await ourUser!.updatePhotoURL(defaultProfileImage);
      notifyListeners();
      return ourUser;
    } on SocketException {
      setIsLoading(false);
      setMessage('No internet');
    } on FirebaseAuthException catch (e) {
      setIsLoading(false);
      setMessage(e.message);
    }

  }

The cause of this issue was displayName and photoURL is unable to be set to null. More details are discussed here. As you've mentioned, this issue should be fixed as of version 1.3.0.

To fetch the display name configured during Registration, you can fetch by calling currentUser on your FirebaseAuth instance.

var currentUser = FirebaseAuth.instance.currentUser;
debugPrint('${currentUser.displayName}');
Related