Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flutter - How do I pop two screens without using named routing?

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.

like image 435
Bigto Avatar asked Jun 23 '19 15:06

Bigto


People also ask

How do I move the monitor from one monitor to another in flutter?

To navigate from one screen to another, we can use Navigator. push(). To go back, we use Navigator. pop().

How do you use POP until in flutter?

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.

How do you navigate between pages in flutter?

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.


2 Answers

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.

like image 197
George Avatar answered Sep 23 '22 23:09

George


You can just pop it two times;

nav = Navigator.of(context);
nav.pop();
nav.pop();
like image 44
LgFranco Avatar answered Sep 24 '22 23:09

LgFranco