【问题标题】:Firebase Auth not saving current userFirebase Auth 不保存当前用户
【发布时间】:2020-08-22 21:31:12
【问题描述】:

在我的应用程序中,我正在尝试执行简单的登录/注册操作。

我有一个StreamBuilder,如果没有用户,则返回登录屏幕,如果用户登录,则返回主屏幕:

StreamBuilder(
          stream: FirebaseAuth.instance.onAuthStateChanged,
          builder: (BuildContext context, AsyncSnapshot snapshot) {
            Widget widget;
            switch (snapshot.data) {
              case (null):
                widget = SignIn();
                break;
              default:
                widget = Home();
            }

            return AnimatedSwitcher(
          child: widget,
          duration: Duration(seconds: 1),
          transitionBuilder: (Widget child, Animation<double> animation) {
            return ScaleTransition(child: child, scale: animation);
          },
        );
          }),

代码完美运行,如下所示:

但是当用户已经登录,并且我关闭并重新打开应用程序时,登录页面会在重定向到主页之前短暂显示。

当我点击热重启时也会发生同样的情况:

在上图中,我已经登录,然后单击热重载,登录屏幕短暂显示,然后被重定向到主屏幕。

当互联网关闭并且没有动画切换器时也会发生同样的情况。

我该如何解决这个问题?

【问题讨论】:

    标签: firebase flutter dart firebase-authentication stream-builder


    【解决方案1】:

    使用ConnectionState:

    if (snapshot.connectionState == ConnectionState.waiting) {
     return Container();
    }
    
    

    您可以将 Container 替换为 SplashScreen/Loading Screen

    【讨论】:

    • 这很有意义!您的代码虽然不起作用,但我调整了我的代码以匹配它并且它确实有效。重启时出现问题的原因是快照的连接状态为“ConnectionState.waiting”。因此,通过在 if(snapshot.connectionState == ConnectionState.waiting) 中添加 return 语句,我解决了问题!谢谢!
    • 是的,总是使用连接状态,好多了
    猜你喜欢
    • 2020-02-04
    • 2020-08-05
    • 2016-12-16
    • 1970-01-01
    • 2023-03-03
    • 1970-01-01
    • 1970-01-01
    • 2019-05-06
    • 1970-01-01
    相关资源
    最近更新 更多