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

Viewed 2225

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
2 Answers

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.

Navigator.of(context).popUntil(ModalRoute.withName('L'));
Related