go_router throws No GoRouter found in context
No GoRouter found in contextShort answer
GoRouter.of(context) looks up the widget tree for the router that MaterialApp.router puts there. It throws when your context sits above that point, such as in a provider wrapped around MaterialApp, or when the app does not use MaterialApp.router with routerConfig. Use the GoRouter object directly, or move the call below the router.
Fix it fast
1Find out which cause you have
Find the line that calls the router with a context, then check where the widget that owns that context sits compared with MaterialApp.router.
grep -rn "context.go\|context.push\|GoRouter.of" lib | head -n 20 grep -rn "MaterialApp" lib | head2Match what you see, then jump to its fix
- 1
The call is in main(), in a provider or bloc created above MaterialApp.router, or in the builder argument of the app widget.The context is above MaterialApp.routerFix - 2
The app starts with a plain MaterialApp(home: ...) and go_router is only used in some screens.The app does not use MaterialApp.router with routerConfigFix
- 1
What does the error look like?
No GoRouter found in contextUnhandled Exception: No GoRouter found in context
Cause 1 of 2
The context is above MaterialApp.router
1Check
In go_router's source, GoRouter.of throws a FlutterError with this message when no InheritedGoRouter is found above the context. That widget is added around the navigator, so only widgets below it can see the router.
grep -rn "BlocProvider\|MultiBlocProvider" lib/main.dart lib/app.dart2Fix
Keep the GoRouter in a variable you create once, and call its methods directly from code that lives above the app. Inside screens, context.go still works.
final GoRouter router = GoRouter(routes: [ /* ... */ ]);
// above the router, no context needed
router.go('/login');
// below the router, in a screen
onPressed: () => context.go('/login'),3Verify
The call runs without the exception, and the app moves to the new location.
flutter runConfirmed by raw.githubusercontent.com
Cause 2 of 2
The app does not use MaterialApp.router with routerConfig
1Check
The go_router getting-started guide says to use MaterialApp.router (or CupertinoApp.router) and set routerConfig to the GoRouter.
grep -rn "MaterialApp\|CupertinoApp\|routerConfig" lib2Fix
Switch to the router constructor and pass your GoRouter as routerConfig.
return MaterialApp.router(
routerConfig: _router,
);3Verify
Hot restart, not hot reload, and open a screen that calls context.go.
flutter runConfirmed by pub.dev
How do I stop it coming back?
- Create the GoRouter once, outside build(), and pass it to routerConfig.
- Use GoRouter.maybeOf(context) in shared code that may run above the router, and handle null.
- Put providers that need navigation below the router, or give them the router object.
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
Dio throws 'status code of 404 and RequestOptions.validateStatus' in Flutter
This exception was thrown because the response has a status code of 404 and RequestOptions.validateStatus was configured to throw for this status code.
Dio throws a DioException for any response outside 2xx and 304, and a 404 is one.
3 causes, each with a fixMobile apps
Dio requests fail with a connection or receive timeout in Flutter
DioExceptionType.connectionTimeout
Dio throws a DioException whose type says which timer ran out: connectionTimeout, sendTimeout or receiveTimeout.
4 causes, each with a fixMobile apps
Dio interceptor throws: the handler has already been called
Bad state: Future already completed
In Dio every interceptor callback gets a handler, and that handler may be completed only once.
2 causes, each with a fixMobile apps
A Dio request in Flutter fails with HTTP 405 Method Not Allowed
405 Method Not Allowed
405 means the server knows the URL but not the HTTP method you used on it.
2 causes, each with a fixMobile apps
Dio POST request ignores the headers you set
The server rejects the body because Content-Type is application/json, not application/x-www-form-urlencoded
Headers belong in Options for one request or in BaseOptions for the whole Dio instance, never in the data map.
3 causes, each with a fixMobile apps
Flutter Dio throws a bad response error with status 422
Unhandled Exception: DioError [DioErrorType.response]: Http status error [422]
A 422 is not a Dio fault.
3 causes, each with a fix