How to pop two screens without using named routing?

Issue

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.

Solution

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.

Answered By – George

Answer Checked By – Jay B. (FlutterFixes Admin)

Leave a Reply

Your email address will not be published. Required fields are marked *