首页 > 解决方案 > 退出 Flutter 应用程序不会返回登录页面

问题描述

我正在使用 firebase 对我的应用程序中的用户进行身份验证,并登录/注册,当我直接在登录页面之后从主页注销时,注销工作正常,但当我从路由的设置页面注销时无法正常工作主页。

它在firebase上注销,但除非我按下appbar上的后退按钮,否则页面不会立即返回登录页面调用signout。

这是我的注销功能:

abstract class BaseAuth {
  Future<void> signOut();
}

Future<void> signOut() async {
  return _firebaseAuth.signOut();
}

final BaseAuth auth;
final VoidCallback onSignedOut;

_signOut() async {
  try {
    await widget.auth.signOut();
    widget.onSignedOut();
  } catch (e) {
    print(e);
  }
}

在我的根页面中,我有:

@override
  Widget build(BuildContext context) {
    switch (authStatus) {
      case AuthStatus.NOT_DETERMINED:
        return _buildWaitingScreen();
        break;
      case AuthStatus.NOT_LOGGED_IN:
        return new LoginSignUpPage(
          auth: widget.auth,
          onSignedIn: _onLoggedIn,
        );
        break;
      case AuthStatus.LOGGED_IN:
        if (_userId.length > 0 && _userId != null) {
          return new HomePage(
            userId: _userId,
            auth: widget.auth,
            onSignedOut: _onSignedOut,
          );
        } else return _buildWaitingScreen();
        break;
      default:
        return _buildWaitingScreen();
    }
  }

因此,如果身份验证状态未登录,它应该返回到 LoginSignUpPage。

我不确定为什么会发生这种延迟。任何帮助,将不胜感激!

标签: firebaseauthenticationflutterfirebase-authentication

解决方案


所以添加Navigator.popUntil(context, ModalRoute.withName("/"));作为最后一行_signOut()解决了这个问题。

它只是弹出堆栈中第一个屏幕顶部的所有屏幕。


推荐阅读