哪种用例在扑朔迷离中具有FirebaseUser的reload()函数? [英] What use case has the reload() function of a FirebaseUser in flutter?

查看:44
本文介绍了哪种用例在扑朔迷离中具有FirebaseUser的reload()函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

FirebaseUser的.reload()方法用于什么?
该函数在本地不会更改任何数据,并且使用Firestore.instance.currentUser()可以始终获取当前数据,对吗?

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?

从文档中

公共任务重新加载()手动刷新当前用户的数据(例如,附加的提供程序,显示名称等).

public Task reload () Manually refreshes the data of the current user (for example, attached providers, display name, and so on).

所以我最初认为在调用user.reload()之后,输出将是:用户名:bar",而不是用户名:foo".所以对我来说似乎什么都没做?

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?

相关的附带问题:
这也意味着我总是必须调用FirebaseAuth.instance.currentUser()以确保必须具有用户的当前信息?没有办法拥有FirebaseUser流,当用户信息更改时,该流会发出新的FirebaseUser吗?(我不是说Firebase.instance.onAuthStateChanged())

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

示例:

 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
  }

控制台输出:

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

推荐答案

这似乎是预期的行为.

就像已经说过的那样,仅调用 .reload 无效.

Like it was already said only calling .reload does not work.

await user.reload();
print(user.emailVerified) // false

相反,您还必须再次获取 currentUser .

Instead you also have to get the currentUser again.

await user.reload();
// `currentUser` is synchronous since FirebaseAuth rework
user = firebaseAuth.currentUser;
print(user.emailVerified) // true

通过返工( firebase_auth:^ 0.18.0 ) FirebaseAuth 也会公开 .userChanges() -Stream.这似乎是首选的解决方案,而不是手动重新加载用户信息.

With the rework (firebase_auth: ^0.18.0) FirebaseAuth does also expose a .userChanges()-Stream. This seems to be the preferred solution instead of manually reloading the user information.

来自 FirebaseAuth.userChanges()的文档:

这是[authStateChanges]和[idTokenChanges]的超集.它提供有关所有用户更改的事件,例如何时使用凭据已链接,未链接以及何时更新用户配置文件.这此Stream的目的是监听实时更新用户无需手动调用[重新加载]然后重新补水更改您的应用程序.

This is a superset of both [authStateChanges] and [idTokenChanges]. It provides events on all user changes, such as when credentials are linked, unlinked and when updates to the user profile are made. The purpose of this Stream is to for listening to realtime updates to the user without manually having to call [reload] and then rehydrating changes to your application.

这还包括 .isEmailVerified afaik.

This includes also .isEmailVerified afaik.

firebaseAuth.userChanges().listen((user) {
  // Your logic
});

这篇关于哪种用例在扑朔迷离中具有FirebaseUser的reload()函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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