flutterdartflutter-navigationflutter-routes

How to navigate to screen and remove all screen/route history [sign out functionality]


How can i navigate to a screen and remove all previous routing history so that the user cannot press back and get to the previous screen?
In my case I want to push the login screen when a user is logged out, but the navigation history is still there so the user can just press back and return to the previous page.

What I have:
A -> B -> C       //Before signing out
A -> B -> C -> L  //After signing out

What I want:
A -> B -> C       //Before signing out
L                 //After signing out

Solution

  • Push to new screen and remove all previous screen

    Navigator.pushAndRemoveUntil(context, MaterialPageRoute(builder: (context) => LoginPage(),), (route) => false);
    

    Push to new route and remove all previous routes

    Navigator.pushNamedAndRemoveUntil(context, login_page_route, (route) => false);
    

    pushAndRemoveUntil will remove all existing routes and push to new page.