How to remove backstack including home page in flutter

Issue

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

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

Answered By – harpreet seera

Answer Checked By – Cary Denson (FlutterFixes Admin)

Leave a Reply

Your email address will not be published. Required fields are marked *