【问题标题】:Reload FirebaseAuth in Flutter在 Flutter 中重新加载 FirebaseAuth
【发布时间】:2021-04-27 17:44:57
【问题描述】:

我在 Flutter 中创建了以下代码:

if (_auth.currentUser != null) {
       &&  FirebaseAuth.instance.currentUser.reload() != null
      Timer(
        Duration(seconds: 3),
        () => Navigator.of(context).pushAndRemoveUntil(
            MaterialPageRoute(
                builder: (context) =>
                    HomeScreen(username: _auth.currentUser.displayName)),
            (Route<dynamic> route) => false),
      );
    } else {
      Timer(Duration(seconds: 4),
          () => Navigator.pushReplacementNamed(context, "/auth"));
    }
  }

问题实际上是部件&amp;&amp; FirebaseAuth.instance.currentUser.reload() != null 不工作。 你现在为什么?每次应用打开时,我都想重新加载 currentUser-Firebase-AuthState。

感谢您的帮助!!!

【问题讨论】:

    标签: firebase flutter firebase-authentication


    【解决方案1】:

    reload() 方法是异步的,并返回一个Future&lt;void&gt;。您应该使用 await 关键字等待未来完成,请参阅 here

    我不是 100% 清楚为什么需要使用 reload() 方法。由于您在代码中使用了displayName 属性,是因为在您的应用程序中,此属性经常更改吗?我建议您阅读此SO answer 中关于为什么应该使用此方法的解释:“调用reload() 从服务器重新加载该用户的配置文件数据”。

    因此,如果您确实需要使用此方法,您可以按照以下方式进行操作:

    void navigate() async {
    
       if (_auth.currentUser != null) {
    
             await _auth.currentUser.reload();
             // continue your business logic here
    
             // For example
             if (_auth.currentUser.isEmailVerified) {
                // Navigate to ...
             }
    
       } else {...}
    
    }
    

    请注意,正如in the FlutterFire doc 所解释的那样,您也可以使用userChanges() 方法:“此stream 提供User 类的实时更新无需调用reload() ,例如何时链接、取消链接凭据以及更新用户的个人资料时”。

    【讨论】:

    • @fynn1307 嗨,你有时间看答案吗?
    猜你喜欢
    • 2021-08-14
    • 2021-11-29
    • 2019-12-04
    • 1970-01-01
    • 1970-01-01
    • 2020-09-13
    • 2019-05-19
    • 2021-12-17
    • 2021-11-17
    相关资源
    最近更新 更多