'rootNavigator: true' causes exception when using pushNamed

Viewed 35

I'm fairly new to Flutter...

I have a home page which uses a BottomNavBar. I'm trying to get a second page to slide in over the first and cover up the BottomNavBar. I've been able to get the functionality I need by using:

Navigator.of(context, rootNavigator: true).push(
  MaterialPageRoute(
    builder: (context) => const SecondPage(),
  ),
);

However, we are trying to use named routes throughout our app. I can successfully navigate to the second page using:

Navigator.pushNamed(context, SecondPage.routeName);

This works but still displays the BottomNavBar.

I get the same result when I use:

Navigator.of(context, rootNavigator: false)
        .pushNamed(SecondPage.routeName);

However, when I use:

Navigator.of(context, rootNavigator: true)
        .pushNamed(SecondPage.routeName);

I get the following exception:

Exception has occurred. FlutterError (Could not find a generator for route RouteSettings("/comments", null) in the _WidgetsAppState. Make sure your root app widget has provided a way to generate this route. Generators for routes are searched for in the following order:

  1. For the "/" route, the "home" property, if non-null, is used.
  2. Otherwise, the "routes" table is used, if it has an entry for the route.
  3. Otherwise, onGenerateRoute is called. It should return a non-null value for any valid route not handled by "home" and "routes".
  4. Finally if all else fails onUnknownRoute is called. Unfortunately, onUnknownRoute was not set.)

For more context, we are using onGenerateRoute in our Navigator:

onGenerateRoute: (settings) {
  switch (settings.name) {
    ...
    case SecondPage.routeName:
      return MaterialPageRoute(
        builder: (_) => const SecondPage(),
      );

I'm at a loss as to why those other cases work as expected but not the one I need. Any ideas?

1 Answers
Related