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'));
Author by
Joel Broström
Updated on December 27, 2022Comments
-
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
-
Javeed Ishaq about 3 yearsstackoverflow.com/questions/45889341/flutter-remove-all-routes kindly check this
-
-
Joel Broström about 3 yearsI'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 about 3 yearsMake sure you are using pop on backpress. Because im using this on every sigout code
-
Joel Broström about 3 yearsI'm not using named routes. Is there an equivalent version for routing worth screen arguments?
-
Joel Broström about 3 yearsYes, 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 about 3 yearsThe 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 about 3 yearsOhh, Keep Growing ^^