Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Logging out of Flutter app doesn't go back to login page

I'm using firebase to authenticate users in my app, and login/signup, and logging out works correctly when I logout from the home page directly following the login page, but doesn't work when I logout from a settings page routed from the home page.

It is logged out on firebase, but the page won't go back to the login page immediately calling signout unless I press the back button on the appbar.

This is my signout function:

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

In my root page, I have:

@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();
    }
  }

So if the authenticaiton status is not logged in, it should return back to the LoginSignUpPage.

I'm not sure why this delay is happening. Any help would be appreciated!

like image 961
fallingriptide Avatar asked Oct 31 '25 06:10

fallingriptide


1 Answers

So adding Navigator.popUntil(context, ModalRoute.withName("/")); as the last line in _signOut() solved the issue.

It simply pop all screens stacked on top of first screen in your stack.

like image 99
flarkmarup Avatar answered Nov 01 '25 20:11

flarkmarup



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!