How to use Navigator.popUntil Flutter

For page_transition plugin

In case you are using named Routes and this Package for transitions between pages (and arguments):

MaterialApp(
 onGenerateRoute: (settings) => {
   switch (settings.name) {
      case "/yourRoute":
        final value = settings.arguments as String?; // only if you pass arguments
        return PageTransition(
          settings: RouteSettings(
            name: "/yourRoute", //HERE is where you name your route for using popUntil
          ),
          child: YourPage(
            parameters: value ?? "null",
          ),
          type: PageTransitionType.fade,
        );

     case "/yourNextRoute":
          ...

     }
   }
),

Edit your main like this to enable calling and using pop with named routes. This would look like this: Navigator.popUntil(context, ModalRoute.withName("/yourRoute")) or Navigator.pushNamed(context, "/yourRoute",arguments: "12345")


Instead of:

          Navigator.of(context).push(
            MaterialPageRoute(
              builder: (context) => Page1(),
            ),
          );

use:

          Navigator.of(context).push(
            MaterialPageRoute(
              settings: RouteSettings(name: "/Page1"),
              builder: (context) => Page1(),
            ),
          );

and then you can use :

Navigator.of(context)
              .popUntil(ModalRoute.withName("/Page1"));

It is duplicate question. Refer this and this.

Basically what is happening is - when you start your app, page1 opens because it goes into the last else and there is no name assigned to it, so when you do popuntil that page name, it doesn't find it at all.

Tags:

Flutter