Ad

Changing Display Name Takes Too Long. Flutter And FIrebase

I'm trying to make a list of my users and the display name takes a bit to change after update profile but by that time the list already has a null in the name section.

final User user = (await
    _auth.createUserWithEmailAndPassword(
      email: emailR,
      password: passwordR,
    )
    ).user;
    user.updateProfile(displayName: usernameR);

    emailc.clear();
    passwordc.clear();
    usernamec.clear();

    DateTime now = DateTime.now();
    String dt = "${now.day.toString().padLeft(2,'0')}/${now.month.toString().padLeft(2,'0')}/${now.year.toString()} ${now.hour.toString()}:${now.minute.toString()}";

    FirebaseFirestore.instance.collection("users").doc(user.uid).set({
      "id": user.uid,
      "name": user.displayName,
      "email": user.email,
      "created_at": dt,
    });

The reason why I think its a timing problem is because on my main page where it says the username the first time when I register, it'll show "hey, null" and every time after that it'll show the correct username.

Ad

Answer

The updateProfile call is an asynchronous operation, and user.displayName won't be updated until that operation has completed. Luckily it returns a Future, so you can just use await on the call to wait for it to complete, in the same way you already do for createUserWithEmailAndPassword.

So:

await user.updateProfile(displayName: usernameR);
Ad
source: stackoverflow.com
Ad