Maaz Zindani

Loading the index…

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

go_router throws No GoRouter found in context

ErrorNo GoRouter found in context

Short 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.

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

Fix it fast

  1. 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 | head
  2. 2Match what you see, then jump to its fix

    1. 1The 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. 2The 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

What does the error look like?

  • No GoRouter found in context
  • Unhandled 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.dart

2Fix

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 run

Confirmed by raw.githubusercontent.com

Did this fix it?

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" lib

2Fix

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 run

Confirmed by pub.dev

Did this fix it?

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:

Related fixes

Mobile 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 fix

Next step

Want this handled for good?

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