Flutter - firebase_auth updateProfile method is no

2020-07-10 08:32发布

问题:

I'm creating app using Flutter with Firebase. I have some weird issues. I'm creating authentication and it is working fine but when i try to add some collections to my Firestore Database, the record of displayName is set to null.

Future<FirebaseUser> createUser(email, password, displayName) async {
    final FirebaseUser user = await _auth.createUserWithEmailAndPassword(
        email: email, password: password);

    UserUpdateInfo info = new UserUpdateInfo();
    info.displayName = displayName;
    _auth.updateProfile(info);

    Firestore.instance.collection('users').document().setData({
      'name': user.displayName,
      'uid': user.uid,
      'email': user.email,
      'isEmailVerified': user.isEmailVerified,
      'photoUrl': user.photoUrl,
    });

    return user;
  } 

this is Future class that creates user.

void _handleSubmitted() {
    userAuth
        .createUser(
        emailController.text, passwordController.text, nameController.text)
        .then((onValue) {
      print("Sign Up button clicked: $onValue");

    });
  }

this method is handle when sign up button is clicked.

and collection looks like this picture.

回答1:

If I recall correctly, the local user profile is not immediately updated when you call updateProfile. This means you should either just write the local values to the database (easiest) or force a reload of the profile (safest).

Write the local values

As said, this is the simplest approach:

Firestore.instance.collection('users').document().setData({
  'name': displayName,
  'uid': user.uid,
  'email': user.email,
  'isEmailVerified': user.isEmailVerified, // will also be false
  'photoUrl': user.photoUrl, // will always be null
});

Note that isEmailVerified will always be false, and photoUrl will always be null on a newly created email+password account.

Force a reload of the profile

You can force a reload of the user data by calling FirebaseUser.reload():

await _auth.updateProfile(info);
await user.reload();
user = _auth.getCurrentUser();


回答2:

To Update users display name and photo url this will definitely helpful.

  FirebaseUser user = await FirebaseAuth.instance.currentUser();

  UserUpdateInfo userUpdateInfo = new UserUpdateInfo();
  userUpdateInfo.displayName = name;
  userUpdateInfo.photoUrl = url;

  user.updateProfile(userUpdateInfo);


回答3:

It's inconvenient the way Firebase Auth works, and they say it won't change for now, so I made a package to address this, grab it here: firebase_user_stream

In the Readme I explain the issues and how the package fixes them, there are examples and etc, enjoy!



回答4:

I also tried everything but however, this works for me! Just call the FirebaseAuth.instance twice like the one in the code.

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

      print("Current User   ${currentUser.displayName}");
      print("Current User   ${currentUser.photoUrl}");

hope this helps you!