32

I have several Page.
1. Start Page
2. Page 2
3. Page 3
4. Main Menu

From 1 -> 2. and 2 -> 3. i use this for navigation :

Navigator.of(context).push(new MaterialPageRoute<MyPage>(
                    builder: (BuildContext context) {
                      return new MyPage();
                    },
                  ));

and for 3 -> 4. I want to use this (Push Replacement, will not going back), but it doesnt work and act like normal Push:

Navigator
        .of(context)
        .pushReplacement(new MaterialPageRoute(builder: (BuildContext context) {
      return new MainMenuPage();
    }));

Confusing.

Arief Wijaya
  • 827
  • 2
  • 8
  • 20

8 Answers8

103

I had the same problem going on, guess it was due to the fact that my screen #3 came from a dialog and i wasn't disposing of it before. So what I did was:

Navigator.of(context).popUntil((route) => route.isFirst);

then

Navigator.pushReplacement(context, MaterialPageRoute(builder: (BuildContext context) => NewPage()));
Fernando Rocha
  • 2,416
  • 3
  • 15
  • 28
  • 3
    It works wonders. I think the Flutter team/contributors should better document what behaviour to expect fro this Navigator API. – Salathiel Genese Apr 22 '20 at 11:02
  • 3
    I didn't know about that first line, either. Together they form a Voltron of "pushNewRoot" which would have been a great addition to the stack management for Routes. – ChrisH Jul 21 '21 at 13:02
  • by name, pushReplacement should do this internally. Yet another lapse in making something Flutter does do it the right way, or at least the expected way according to the name. – ChrisH Apr 19 '22 at 17:36
11

Well, pushReplacement() isn't supposed to stop you from going back to any page. As comes from its name, it Replaces the new page that you are trying to open with the current page in the routes stack.

For example, when you are in the second page, use the pushReplacement() method and open the third page, when you hit the back button you will go to the first page and skip the second page.

To make it more clear you can navigate to the forth page like this:

[ 1 --push()--> 2 --pushReplacement()--> 3 --pushReplacement()--> 4] and when you hit the back button you will get to the first page.

Now if you want to control the backButton in android you can use the WillPopScope() method.

Bink
  • 1,934
  • 1
  • 25
  • 41
Taba
  • 3,850
  • 4
  • 36
  • 51
6

Try this

Navigator.of(context).pushNamedAndRemoveUntil('/routeName', (route) => false);
MendelG
  • 14,885
  • 4
  • 25
  • 52
Eray Hamurlu
  • 657
  • 7
  • 9
2

Now i'm using alternative way, Using pushReplacement instead of push for navigate and add WillPopScope for going back. So when i reach screen no 3. The replacement is work.

Arief Wijaya
  • 827
  • 2
  • 8
  • 20
1

i think that you don't want to go with screen no 3 if the user is once reach screen no 4 then you can use Navigator.pop(context); before the transferring the control to screen no 4.

Viren V Varasadiya
  • 25,492
  • 9
  • 45
  • 61
  • 1
    Doesn't work. when i'm using Navigator.pop(context); it's reach screen no 2 with showDialog opened – Arief Wijaya Jul 23 '18 at 23:51
  • Now i'm using alternative way, Using pushReplacement instead of push for navigate and add WillPopScope for going back. So when i reach screen no 3. The replacement is work. Is this a good way to achieve that or i was wrong ? – Arief Wijaya Jul 24 '18 at 00:18
1

Please try pushAndRemoveUntil(), that is how I solve this.

 Navigator.of(context).pushAndRemoveUntil(
                                          MaterialPageRoute(
                                              builder: (context) => HomePage()),
                                              (route) => false
                                        );
Hedron Dantas
  • 616
  • 2
  • 6
  • 16
0

Albeit your WillPopScope solution will work and is the appropriate solution for some navigation workflows, I am finding that the root of the problem is gaining better control over the Flutter Navigator, particular ensuring that the Back button and what should be on the Navigator stack, at any given User Action within an app is using Named Routes for all Navigations, this article is a great guide, to declaring Routes and then using named Routes with the Navigator to ensure that Navigator stack always contains the exact routes you want for any given navigation flow:

https://medium.com/flutter-community/flutter-push-pop-push-1bb718b13c31

Brian Ogden
  • 18,439
  • 10
  • 97
  • 176
0

I had an issue similer to this, I navigate from 1(Page) to 2(Form with Will pop) to 3(Page) I want to go from 3 to 1 (without seeing the Form page)

so I used this

  Navigator.of(context).push(
            MaterialPageRoute(
                builder: (_) => GenericPage(),
                )).then((value) => Navigator.pop(context));
Ramy Wahid
  • 218
  • 2
  • 6