Flutter throws 'Could not find a generator for route' on Navigator.pushNamed
Could 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.
Fix it fast
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/2Match what you see, then jump to its fix
- 1
RouteSettings("/profile", null) while the table only has '/profil' or 'profile'The name pushed is not a key of the routes tableFix - 2
Navigator.onGenerateRoute returned null when requested to build routeonGenerateRoute returns null for a name it does not handleFix - 3
Navigator.onGenerateRoute was null, but the route named "..." was referenced.A nested Navigator has no handler of its ownFix
- 1
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 runConfirmed by raw.githubusercontent.com
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.dart2Fix
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 runConfirmed by api.flutter.dev
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 runConfirmed by raw.githubusercontent.com
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:
- TaxiDexMy own multi-tenant taxi dispatch SaaS: API, dispatcher portal, driver and passenger apps, and the servers underneath.
- Qist BazaarA buy-now-pay-later marketplace built from a blank repo, now past 100K downloads on Google Play.
- SessionBirdAn omnichannel engagement suite in Flutter: campaigns, chatbot, tickets, analytics and live calling.
Related fixes
Mobile apps
AudioCache has no play method after updating audioplayers
The method 'play' isn't defined for the type 'AudioCache'.
Since audioplayers 1.0, AudioCache only turns asset files into local files and caches them.
2 causes, each with a fixMobile apps
Flutter Bloc throws that emit was called after the event handler completed
emit was called after an event handler completed normally.
Each on<Event> handler gets its own Emitter, and it stops working the moment the handler's returned Future completes.
2 causes, each with a fixMobile apps
analysis_options.yaml: the URI it includes can't be found
include_file_not_found
The analyzer reports include_file_not_found when the file named after include: in analysis_options.yaml does not resolve.
2 causes, each with a fixMobile apps
Dart error: the body might complete normally and return null for a non-nullable type
body_might_complete_normally
Dart found a path through your function or callback that reaches the closing brace without a return.
3 causes, each with a fixMobile apps
Dart throws ConcurrentModificationError while looping over a list
ConcurrentModificationError
Dart throws this when a collection changes size while something iterates it.
2 causes, each with a fixMobile apps
Dart cannot turn a compact timestamp like 20201101153000 into a DateTime
FormatException: Invalid date format
DateTime.parse accepts a subset of ISO 8601, and a time part must be introduced by T or a space.
2 causes, each with a fix