JSA*_* L. 5 dart firebase firebase-authentication flutter
What is the method .reload() from the FirebaseUser used for?
Locally the function doesn't change any data, and with Firestore.instance.currentUser() i would always get the current data, wouldn't I?
From the docs:
public Task reload () Manually refreshes the data of the current user (for example, attached providers, display name, and so on).
So I originally thought after calling user.reload() the output would be: "name of user: bar" and not "name of user: foo". So for me it seems like it doesn't really do anything?
Related side-question:
Also that means that I always have to call FirebaseAuth.instance.currentUser() to be sure to have to current information of the user? There's no way to have a stream of FirebaseUser, which emits a new FirebaseUser when user information is changed? (I don't mean Firebase.instance.onAuthStateChanged() )
Example:
static stackOverflowProblem() async {
FirebaseUser user = await FirebaseAuth.instance.currentUser();
print("Name of User: ${user.displayName}"); //prints foo
//Renaming the name from "foo" to "bar"
UserUpdateInfo userInfo = UserUpdateInfo();
userInfo.displayName = "bar";
await _auth.updateProfile(userInfo);
print("\nBefore calling user.reload:");
print("Name of user: ${user.displayName}"); //prints foo
print("Name of FirebaseAuth.instance.currentUser: ${(await FirebaseAuth.instance.currentUser()).displayName}"); //prints bar
await user.reload();
print("\nAfter calling user.reload:");
print("Name of user: ${user.displayName}"); //prints foo
print("Name of FirebaseAuth.instance.currentUser: ${(await FirebaseAuth.instance.currentUser()).displayName}"); //prints bar
}
Run Code Online (Sandbox Code Playgroud)
Console output:
I/flutter (19989): Name of User: Foo
I/flutter (19989):
I/flutter (19989): Before calling user.reload:
I/flutter (19989): Name of user: Foo
I/flutter (19989): Name of FirebaseAuth.instance.currentUser: bar
I/flutter (19989):
I/flutter (19989): After calling user.reload:
I/flutter (19989): Name of user: Foo
I/flutter (19989): Name of FirebaseAuth.instance.currentUser: bar
Run Code Online (Sandbox Code Playgroud)
让您无需注销和重新登录即可验证电子邮件。但它可能是一个错误,但是当您调用 user.reload() 时,您必须再次调用 currentUser()。
这不起作用:
await user.reload();
user.isEmailVerified => still false
Run Code Online (Sandbox Code Playgroud)
你需要:
await user.reload();
user = await _auth.currentUser();
user.isEmailVerified => now it is true
Run Code Online (Sandbox Code Playgroud)
调用会User.reload()从服务器重新加载该用户的配置文件数据。
一个典型的用例是当您向用户发送电子邮件以验证他们的电子邮件地址时。当用户点击该电子邮件中的链接时,它会返回到 Firebase 身份验证服务器,该服务器将他们的电子邮件地址标记为已验证。但是当发生这种情况时,您的应用程序无法知道它,因为调用直接从电子邮件客户端发送到服务器。通常你会向链接添加一个所谓的继续 URL,它可以在电子邮件地址被验证后回调到你的应用程序。然后您的应用程序可以调用reload()从服务器加载更新的状态。
| 归档时间: |
|
| 查看次数: |
596 次 |
| 最近记录: |