Flutter Navigator popUntil method doesn't stop at the Named Route I want it to, and pops all the screen


I have this bit of code that I want to pop all screens until it gets to the base screen with Name "/".


Before I call the popUntil method, I navigated using:


But the result I’m getting is all the screens are getting popped until it gets to the black screen. What should I change to make it stop at "/"?

Here is how it’s set up:


class MyApp extends StatelessWidget {
  const MyApp({Key? key}) : super(key: key);

  Widget build(BuildContext context) {
    return MaterialApp(
        title: 'My App',
        theme: ThemeData(
          primarySwatch: Colors.blue,
        debugShowCheckedModeBanner: false,
        onGenerateRoute: AppRouter().onGenerateRoute,
        initialRoute: '/',

class AppRouter {
  Route? onGenerateRoute(RouteSettings routeSettings) {
    switch (routeSettings.name) {
      case '/':
        return MaterialPageRoute(builder: (_) => const LoadingScreen());
      case '/Menu':
        return MaterialPageRoute(builder: (_) => const MenuScreen());
      case '/Loading':
        return MaterialPageRoute(builder: (_) => const LoadingScreen());


The ModalRoute.withName predicate is used when a route is tied to a specific route name. Because you’re using onGenerateRoute (which is typically a last resort) instead of the routes table in your MaterialApp there is no route associated with the / route name.

Answered By – Nico Spencer

Answer Checked By – Timothy Miller (FlutterFixes Admin)

Leave a Reply

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