Welcome to MLink Developer Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
975 views
in Technique[技术] by (71.8m points)

flutter - Firebase displayName is only shown after Re-login not at the first login

I have 2 methods. One that creates user called signUp and other that adds display name to user profile called updatesUser. During the signUp process, I am calling updateUser method as well. Once registration is successful, the user is sent to HomeScreen.

However, on signUp completion when user is at HomeScreen, the display name is shown as "null". But then it works when the same user logs out and comes back in.

User object and refs are defined in parent class of these methods:

class FirebaseController extends GetxController {
  FirebaseAuth _auth = FirebaseAuth.instance;
  Rx<User> _firebaseUser = Rx<User>();
  String get user => _firebaseUser.value?.email;
  String get userid => _firebaseUser.value?.uid;
  String get displayName => _firebaseUser.value?.displayName;



void signUp(String username, String email, String password) async {
    CollectionReference dbref = FirebaseFirestore.instance.collection("users");

    await _auth
        .createUserWithEmailAndPassword(email: email, password: password)
        .then((value) {
      dbref
          .doc(userid)
          .set({
            "username": username,
            "email": email,
            "password": password,
            "uid": userid,
           })
          .then((value) => updateUser(username: username))
          .then((value) => Get.offAll(HomeScreen()));
    }).catchError(
      (onError) =>
          Get.snackbar("Error while creating account ", onError.message),
    );
  }

This is my updateUser method that is called during "signUp"

void updateUser({username}) async {
    await _auth.currentUser
        .updateProfile(displayName: username)
        .then((value) => _firebaseUser.value.reload())
        .then((value) => Get.snackbar("User Created", "User has been created"))
        .catchError((onError) =>
            Get.snackbar("Error while Updating DisplayName ", onError.message));
  }

What am I doing Wrong?!


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

It's not about what you're doing wrong. It's about what you're not doing at all.

Updating the user profile does not update the User object for the currently signed in user. If you want to see changes to the new profile, you have to use reload() on that user object.

See also:


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to MLink Developer Q&A Community for programmer and developer-Open, Learning and Share
...