How to solve: Flutter Navigator - push to the same page

Question

Asked by sosnus on November 04, 2021 (source).

how is the best way to push to the same page?

I have route A>B>C

and now, at page C I want do action, and after it I want push to C, but remove old C from route, so:

I have: A>B>C

And execute this code on Page C

Navigator.of(context).pushReplacement(
                  MaterialPageRoute(
                    builder: (context) => C(param: myParam),
                  ),
                );

And I have A>B>C>C How to change this code to get new route ' A>B>C' after second push to page C? I just want delete last navigation from route

Answer

Question answered by quim (source).

If you are in the same page you want to push, you could do:

Navigator.popAndPushNamed(context, C);

Which will pop current page (C) and push C.

FLUTTER
SHARE: