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

1,377

Solution 1

Use below code

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

Change HomePage() to your class name "L".

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

Solution 2

Navigator.of(context).popUntil(ModalRoute.withName('L'));
Share:
1,377
Joel Broström
Author by

Joel Broström

Updated on December 27, 2022

Comments

  • Joel Broström
    Joel Broström over 1 year

    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
    
  • Joel Broström
    Joel Broström about 3 years
    I've seen this suggestion before, but when I execute the code I land on the page after the login: A -> B -> C becomes L -> A. If I press back I will return to the login page.
  • Priyesh
    Priyesh about 3 years
    Make sure you are using pop on backpress. Because im using this on every sigout code
  • Joel Broström
    Joel Broström about 3 years
    I'm not using named routes. Is there an equivalent version for routing worth screen arguments?
  • Joel Broström
    Joel Broström about 3 years
    Yes, it also has 290+ upvotes in the other PR, so it should work. What do you mean with pop on backpress. Isn't that handled by the flutter navigation framework?
  • Joel Broström
    Joel Broström about 3 years
    The code you provided works, it's my firebase connection that had little bit of lag so when i signed out and checked my auth status it stil said authenticated and proceeded to login. Thanks for your help buddy!
  • Priyesh
    Priyesh about 3 years
    Ohh, Keep Growing ^^