For example, my current routing is like this:
Login -> Screen1 -> Screen2 -> Screen3 -> Screen4
I'd like to go back to Screen2
from Screen4
.
I can't use named routing, because I have to pass a parameter to Screen2
.
Push Screen2
in Screen4
is not a good solution.
To navigate from one screen to another, we can use Navigator. push(). To go back, we use Navigator. pop().
Calls pop repeatedly on the navigator that most tightly encloses the given context until the predicate returns true. The predicate may be applied to the same route more than once if Route. willHandlePopInternally is true. To pop until a route with a certain name, use the RoutePredicate returned from ModalRoute.
The basic way — push and pop The Navigator behaves like a stack, so the most simple way is to push a new page onto it when you want to navigate to it and to pop a page if you want to go back.
Use popUntil
method of Navigator class.
e.g.
int count = 0;
Navigator.of(context).popUntil((_) => count++ >= 2);
However, I would recommend defining names for your routes and using popUntil
as it is designed as per docs.
You can just pop it two times;
nav = Navigator.of(context);
nav.pop();
nav.pop();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With