Firebase Auth 不保存当前用户

Firebase Auth not saving current user

在我的应用程序中,我正在尝试执行一个简单的 login/signup 操作。

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

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);
          },
        );
          }),

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

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

当我点击热重启时同样发生:

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

网络关闭时也会出现同样的情况,并且没有动画切换器。

我该如何解决这个问题?

使用ConnectionState:

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

您可以用 SplashScreen/Loading 屏幕替换 Container