简体   繁体   English

Flutter - firebase_auth updateProfile 方法不起作用

[英]Flutter - firebase_auth updateProfile method is not working

I'm creating app using Flutter with Firebase.我正在使用 Flutter 和 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.我正在创建身份验证并且它工作正常但是当我尝试将一些集合添加到我的 Firestore 数据库时,displayName 的记录设置为 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.这是创建用户的 Future 类。

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.和收藏看起来像这张照片。

收藏

If I recall correctly, the local user profile is not immediately updated when you call updateProfile .如果我没记错的话,当您调用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.请注意, isEmailVerified新创建的电子邮件+密码帐户, isEmailVerified将始终为 false,而photoUrl将始终为null

Force a reload of the profile强制重新加载配置文件

You can force a reload of the user data by calling FirebaseUser.reload() :您可以通过调用FirebaseUser.reload()强制重新加载用户数据:

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

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

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 Firebase Auth 的工作方式很不方便,他们说现在不会改变,所以我制作了一个包来解决这个问题,在这里获取: firebase_user_stream

In the Readme I explain the issues and how the package fixes them, there are examples and etc, enjoy!在自述文件中,我解释了问题以及软件包如何修复它们,还有示例等,尽情享受吧!

EDIT (Jul/2020):编辑(2020 年 7 月):

Firebase Auth for Flutter now has a userChanges stream: Flutter 的 Firebase 身份验证现在有一个userChanges流:

which triggers whenever auth state, id token or profile changes occur.每当身份验证状态、id 令牌或配置文件发生更改时都会触发。 Essentially, this acts as a way to obtain realtime changes on currently stateless functionality (such as updateProfile).本质上,这是一种获取当前无状态功能(例如 updateProfile)的实时更改的方法。

I also tried everything but however, this works for me!我也尝试了一切,但是,这对我有用! Just call the FirebaseAuth.instance twice like the one in the code.只需像代码中那样调用 FirebaseAuth.instance 两次即可。

 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!希望这对你有帮助!

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Flutter firebase_auth signInWithCredential 不适用于 android - Flutter firebase_auth signInWithCredential is not working on android firebase_auth插件升级到0.6.2 + 1后,缺少_firebaseAuth.updateProfile - _firebaseAuth.updateProfile missing after firebase_auth plugin upgrade to 0.6.2+1 Dart Flutter 顺序 firebase_auth 错误 - Dart Flutter sequential firebase_auth errors Flutter Web:MissingPluginException(在频道 plugins.flutter.io/firebase_auth 上找不到方法 startListeningAuthState 的实现) - Flutter Web: MissingPluginException(No implementation found for method startListeningAuthState on channel plugins.flutter.io/firebase_auth) MethodChannel#plugins.flutter.io/firebase_auth(31559):无法处理方法调用 - MethodChannel#plugins.flutter.io/firebase_auth(31559): Failed to handle method call MissingPluginException(在通道插件上找不到方法 signInAnonymously 的实现。flutter.io/firebase_auth) - MissingPluginException(No implementation found for method signInAnonymously on channel plugins.flutter.io/firebase_auth) Flutter&Firebase-与firebase_auth一起使用firebase_storage - Flutter & Firebase - using firebase_storage along with firebase_auth MissingPluginException(在通道 plugins.flutter.io/firebase_auth 上没有找到方法 signInWithCredential 的实现) - MissingPluginException(No implementation found for method signInWithCredential on channel plugins.flutter.io/firebase_auth) Firebase_auth 无法在 flutter [firebase_auth: ^0.16.1] 上构建发布 apk - Firebase_auth cannot build release apk on flutter [firebase_auth: ^0.16.1] Firebase身份验证:Cloud Function中的updateProfile? - Firebase Auth: updateProfile in Cloud Function?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM