问题
I have two options for user login,
- using email and password
- using google sign in
When I log using google sign in method its display name, but it not display name when using email and password, user.displayName is return null when using 1st option. I have use provider for authenticating, I have tried currentUser method also. I store users in firestore. This is what I am getting.This is my users collection
回答1:
If a user signed up using email and password, there is an option to update displayName and photoUrl of the user.
// Suppose currentUser is the logged in FirebaseUser
currentUser.updateProfile(
UserUpdateInfo(
displayName: 'name',
photoUrl: '...',
),
),
If this was never done displayName and photoUrl will return null
Since you have data in users collection you'll have to access that data from that document just like u access any other document in firestore.
A better approach would be to accept displayName during sign up so that u can update displayName of that user:
FirebaseAuth.instance.createUserWithEmailAndPassword(
email: 'an email',
password: 'a password',
).then((newUser) {
newUser.updateProfile(
UserUpdateInfo(
displayName: 'name',
),
);
});
Now u can access using currentUser.displayName directly.
来源:https://stackoverflow.com/questions/63202102/displayname-return-null-when-logged-using-email-and-password