3
votes

Below is my app widget tree. If user is not logged in then login page is shown if user is logged in then work page. on account page I have logout button.Logout button implements push replacement and shows login page.

Issue is - When user click on back button again work page is shown. How to remove all the routes from widget tree and only show login page after logout action?

Note - I am not using Named routes, just Push , Pop and Replacement

enter image description here

2

2 Answers

3
votes

If you are using namedRoutes, you can do this by simply :

Navigator.pushNamedAndRemoveUntil(context, "/login", (Route<dynamic> route) => false);

Where "/login" is the route you want to push on the route stack.

Note That :

This statement removes all the routes in the stack and makes the pushed one the root.

1
votes

Close your dialog by calling,

Navigator.pop(context);

and then call pushReplacement

void _doOpenPage() {
  navigator.pushReplacement(
      MaterialPageRoute(builder: (BuildContext context) => MyHomePage()));
}

pushReplacement Replace the current route of the navigator by pushing the given route and then disposing the previous route once the new route has finished animating in.

Read More here