[英]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).这意味着您应该只将本地值写入数据库(最简单)或强制重新加载配置文件(最安全)。
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
。
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.