Search code examples
iosswiftflutterdartuinavigationcontroller

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.


Solution

  • 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';
    });