'How to remove backstack including home page in flutter
I have seen many examples but none of them providing me a way to remove the entire back stack(including the home page) while navigating to the next page.
Eg: I have a few login pages once a user successfully entered login credentials user move to the home screen, so here I want to remove all previous screen which appeared till now, How can I do that?
Currently using code:
Navigator.of(context).pushNamedAndRemoveUntil(
HomeScreen.route_name, ModalRoute.withName('/'));
Solution 1:[1]
To remove all the routes below the pushed route, use a RoutePredicate that always returns false (e.g. (Route route) => false).
So for your code to work as expected, remove ModalRoute.withName('/') and give a route predicate which returns false. So it should be
Navigator.of(context).pushNamedAndRemoveUntil(
HomeScreen.route_name, (Route<dynamic> route)=>false);
For reference see the official documentation
Solution 2:[2]
Try this out. This code push the given route onto the navigator and then remove all the previous routes even HomeScreen.
Navigator.pushAndRemoveUntil(
context,
MaterialPageRoute(
builder: (BuildContext context) => HomeScreen(),
),
(route) => false,
);
Note: This is tested on my project. So I hope it will 100% work
Solution 3:[3]
Try this way
Navigator.pushAndRemoveUntil(
context,
MaterialPageRoute(builder: (dialogContex) => HomePageScreen()),
ModalRoute.withName("/HomePageScreen"));
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | harpreet seera |
Solution 2 | Subarata Talukder |
Solution 3 | AskNilesh |