displayName return null when logged using email and password

烈酒焚心 提交于 2021-02-11 18:14:30

问题


I have two options for user login,

  1. using email and password
  2. 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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!