Rohan Mehra
Rohan Mehra

Reputation: 182

How can I pop to specific screen in flutter

I pushed four screens ScreenOne >> ScreenTwo >> ScreenThree >> ScreenFour

and I'm at ScreenFour now I want to pop back to ScreenTwo

In Swift it works like this::

if let viewControllers: [UIViewController] = self.navigationController!.viewControllers {
    for vc in viewControllers
    {
        if (vc is SecondViewController)
        {
            self.navigationController!.popToViewController(vc, animated: true)
        }
    }
}

how i perform this operation in flutter.

Please give a solution, Thank you.

Upvotes: 14

Views: 11929

Answers (5)

Samuel Ayeni
Samuel Ayeni

Reputation: 1

While the pushNamedAndRemoveUntil might seem like a great idea for unnamed routes, it would give a blank screen if you try to pop any further for pop-capable screen. Unless you're returning to the first screen, it is better to just use the default Navigator.pop(context)

Upvotes: 0

Vishal Agrawal
Vishal Agrawal

Reputation: 354

If you don't want to use named route then simply use-- pushAndRemoveUntil

Example--

Navigator.pushAndRemoveUntil(context, MaterialPageRoute(
                          builder: (context) => MyHomePage()), (route) => false);

Upvotes: 4

Shakti S.P. Swain
Shakti S.P. Swain

Reputation: 453

If you didn't define any route in the MaterialApp then you need to define at the time of push.

Navigator.of(context).push(
    MaterialPageRoute(builder: (_) {
      return SecondPage();
    },
      settings: RouteSettings(name: 'SecondPage',),
    ));

You need to define the same route name

Navigator.of(context).popUntil((route){
  return route.settings.name == 'SecondPage';
}); 

Upvotes: 23

Ravinder Kumar
Ravinder Kumar

Reputation: 8010

try pushNamedAndRemoveUntil,

        Navigator.of(context).pushNamedAndRemoveUntil(
            '/BottomNavigation', (Route<dynamic> route) => false);

For in depth reading follow this

Upvotes: 3

Tomisin
Tomisin

Reputation: 61

I believe that would be the Navigator.popUntil Method.

You need to have your routes defined in your materialApp for Navigator to recognise it. For the code below to work "/home" needs to be defined in my MaterialApp.

Navigator.popUntil(context, ModalRoute.withName('/home'));

Upvotes: 3

Related Questions