Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Flutter and app store releases

Flutter throws 'Could not find a generator for route' on Navigator.pushNamed

ErrorCould not find a generator for route RouteSettings(...) in the _WidgetsAppState.

Short answer

MaterialApp looks a route name up in four places in order: home for '/', the routes table, onGenerateRoute, then onUnknownRoute. This error means all four came back empty and no onUnknownRoute was set. Print the name being pushed, compare it with your table, and add an onUnknownRoute so a bad name shows a page, not a crash.

  • Applies to Flutter 3.50 stable, Dart 3.x
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    Read the route name inside RouteSettings(...) in the error. That string is exactly what the app asked for, so compare it character by character with the keys you registered.

    grep -rn "pushNamed\|routes:\|onGenerateRoute\|initialRoute" lib/
  2. 2Match what you see, then jump to its fix

    1. 1RouteSettings("/profile", null) while the table only has '/profil' or 'profile'The name pushed is not a key of the routes tableFix
    2. 2Navigator.onGenerateRoute returned null when requested to build routeonGenerateRoute returns null for a name it does not handleFix
    3. 3Navigator.onGenerateRoute was null, but the route named "..." was referenced.A nested Navigator has no handler of its ownFix

What does the error look like?

  • Could not find a generator for route RouteSettings(...) in the _WidgetsAppState.
  • Unfortunately, onUnknownRoute was not set.
  • Navigator.onGenerateRoute returned null when requested to build route

Cause 1 of 3

The name pushed is not a key of the routes table

1Check

The app resolves a name in a fixed order: home for the default route '/', then the routes map, then onGenerateRoute. A missing slash, a different case or a stale name after a rename all fall through every step.

grep -n "routes:" -A 8 lib/main.dart
grep -rn "pushNamed(" lib/

2Fix

Use one constant per route name and use it on both sides, so the table and the push call cannot drift apart.

class Routes {
  static const home = '/';
  static const profile = '/profile';
}

MaterialApp(
  routes: {
    Routes.home: (_) => const HomePage(),
    Routes.profile: (_) => const ProfilePage(),
  },
);

Navigator.pushNamed(context, Routes.profile);

3Verify

Hot restart and open the screen that failed. It should open with no red error screen.

flutter run

Confirmed by raw.githubusercontent.com

Did this fix it?

Cause 2 of 3

onGenerateRoute returns null for a name it does not handle

1Check

A switch inside onGenerateRoute that has no default branch returns null for any other name. Flutter then falls through to onUnknownRoute, and throws when there is none.

grep -n "onGenerateRoute" -A 15 lib/main.dart

2Fix

Return a route from every branch, and add a default branch. Better, give the app an onUnknownRoute, which is the documented place for a not-found page.

MaterialApp(
  onGenerateRoute: (settings) {
    switch (settings.name) {
      case '/':
        return MaterialPageRoute(builder: (_) => const HomePage());
      case '/profile':
        return MaterialPageRoute(builder: (_) => const ProfilePage());
    }
    return null; // handed to onUnknownRoute
  },
  onUnknownRoute: (settings) => MaterialPageRoute(
    builder: (_) => const NotFoundPage(),
  ),
);

3Verify

Push a name that does not exist, for example Navigator.pushNamed(context, '/nope'). The not-found page should appear.

flutter run

Confirmed by api.flutter.dev

Did this fix it?

Cause 3 of 3

A nested Navigator has no handler of its own

1Check

A Navigator you create yourself inside a widget (for tabs or a wizard) has its own onGenerateRoute, which is null unless you set it. Calling pushNamed on a context under that Navigator uses it.

grep -rn "Navigator(" lib/

2Fix

Give the inner Navigator an onGenerateRoute, or push with a route object (Navigator.push with MaterialPageRoute) instead of a name.

Navigator(
  onGenerateRoute: (settings) => MaterialPageRoute(
    builder: (_) => const StepOnePage(),
    settings: settings,
  ),
);

3Verify

Trigger the same push again; the inner Navigator should show the page.

flutter run

Confirmed by raw.githubusercontent.com

Did this fix it?

How do I stop it coming back?

  • Keep route names as constants in one file and never type a route string twice.
  • Always set onUnknownRoute so a wrong name shows a not-found page.
  • After renaming a route, search the project for the old string.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Projects of mine built on the same technologies:

Related fixes

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.