使用电子邮件和密码登录时,displayName返回null [英] displayName return null when logged using email and password

查看:52
本文介绍了使用电子邮件和密码登录时,displayName返回null的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个用户登录选项,

I have two options for user login,

  1. 使用电子邮件和密码
  2. 使用google登录

当我使用google登录方法登录时,其显示名称,但在使用电子邮件和密码时不显示名称,当使用第一个选项时,user.displayName返回null.我使用提供程序进行身份验证,也尝试过currentUser方法.我将用户存储在Firestore中.这就是我要得到的.

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

推荐答案

如果用户使用电子邮件和密码注册,则可以选择更新 displayName photoUrl 用户.

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: '...',
  ),
),

如果从未执行过此操作,则 displayName photoUrl 将返回 null

If this was never done displayName and photoUrl will return null

由于您在 users 集合中有数据,因此您将必须从该 document 中访问该数据,就像您访问其中的任何其他 document 消防站.

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.

一种更好的方法是在注册期间接受 displayName ,这样您就可以更新该用户的 displayName :

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',
    ),
  );
});

现在,您可以直接使用 currentUser.displayName 进行访问.

Now u can access using currentUser.displayName directly.

这篇关于使用电子邮件和密码登录时,displayName返回null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆