how to switch to a new root in flutter without saving the old one in the stack?

Viewed 702

based on the documentation :

To switch to a new route, use the Navigator.push() method. The push() method adds a Route to the stack of routes managed by the Navigator.

but this will cause an issue as if I click the back button it will get me back to the first screen root , so how I prevent that ? when i change the root i don't want the old one to be saved in the root stack

Navigator.push(
          context, MaterialPageRoute(builder: (context) => HomeScreen()));
1 Answers

Use

Navigator.pushReplacement(context, MaterialPageRoute(builder: (context) => HomeScreen()))

From the docs we see this method will

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.

Related