Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to clear the navigation stack in Flutter

Tags:

flutter

I want my application to a have Welcome screen, with registration and login option. When I tap on "registration" button to navigate to my Registration screen, i use:

Navigator.push

When I press the "back" button within the Registration screen, it must return to the Welcome page.

But after the user completes the registration I'm using the following code in the Homepage to navigate to my home.

Navigator.pushReplacement

The problem is, when I pressed the "back" button, my app is coming back to the Welcome screen, instead of staying within the Home.

Any idea what is going on?

like image 675
Alun Avatar asked Dec 10 '22 01:12

Alun


1 Answers

You can use the following method to clear the navigation stack when you navigate:

Navigator.of(ctx).pushAndRemoveUntil(YourRoute, (Route<dynamic> route) => false);

The second parameter is a Predicate to decide weather the route will be deleted from the stack or not.

Using this method, you'll clear the whole stack before navigating to the route, that way, you won't be able to go back to your Welcome page.

like image 95
Marcelo Wippel Avatar answered Jan 17 '23 23:01

Marcelo Wippel