Flutter-firebase_auth updateProfile方法不起作用 [英] Flutter - firebase_auth updateProfile method is not working

查看:80
本文介绍了Flutter-firebase_auth updateProfile方法不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Flutter和Firebase创建应用程序。我有一些奇怪的问题。我正在创建身份验证,并且工作正常,但是当我尝试向Firestore数据库中添加一些集合时,displayName的记录设置为null。

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;
  } 

这是创建用户的Future类。

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.

推荐答案

如果我没记错的话,当您调用 updateProfile 时,本地用户配置文件不会立即更新。 。这意味着您应该只将本地值写入数据库(最简单),或者强制重新加载配置文件(最安全)。

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).

如上所述,这是最简单的方法:

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

请注意, isEmailVerified 始终为假,和 photoUrl 在新创建的电子邮件+密码帐户上始终为 null

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

您可以通过调用 FirebaseUser.reload()

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

这篇关于Flutter-firebase_auth updateProfile方法不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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