Maaz Zindani

Loading the index…

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

Flutter says the context does not include a Navigator

ErrorNavigator operation requested with a context that does not include a Navigator.

Short answer

Navigator.of looks upward from the context you pass. If that context belongs to a widget above the Navigator, usually the widget that builds MaterialApp itself, nothing is found and Flutter throws. Use a context from a widget below MaterialApp, for example through a Builder, or navigate with a navigatorKey.

  • Applies to Flutter 3.x 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 Navigator.of or Navigator.push and note which build method its context comes from. If that method also returns MaterialApp or CupertinoApp, you have the cause.

    grep -rn "Navigator\.\(of\|push\|pop\)" lib/ | head -20
  2. 2Match what you see, then jump to its fix

    1. 1The call sits in the same build method that returns MaterialApp, or in its home: argumentThe context comes from the widget that builds MaterialAppFix
    2. 2A service, view model or notification handler calls Navigator with no screen contextThe code runs where no BuildContext existsFix

What does the error look like?

  • Navigator operation requested with a context that does not include a Navigator.
  • The context used to push or pop routes from the Navigator must be that of a widget that is a descendant of a Navigator widget.

Cause 1 of 2

The context comes from the widget that builds MaterialApp

1Check

Navigator.of returns the closest Navigator above the context. The Navigator is created inside MaterialApp, so a context from the widget that creates MaterialApp is above it and finds none.

grep -rn "MaterialApp" lib/

2Fix

Move the code that navigates into a widget below MaterialApp, such as a separate screen widget. If you want to keep it inline, wrap it in a Builder: it adds one more element and gives you a context from below.

home: Builder(
  builder: (context) => TextButton(
    onPressed: () => Navigator.of(context).push(
      MaterialPageRoute(builder: (_) => const NextPage()),
    ),
    child: const Text('Next'),
  ),
),

3Verify

Tap the button; the new route opens and the error is gone.

flutter run

Confirmed by api.flutter.dev

Did this fix it?

Cause 2 of 2

The code runs where no BuildContext exists

1Check

Navigator.of needs a context that sits below a Navigator. Code outside the widget tree has none to give it.

grep -rn "navigatorKey" lib/

2Fix

Give MaterialApp a GlobalKey<NavigatorState> and navigate through its currentState. The key replaces the context lookup.

final navigatorKey = GlobalKey<NavigatorState>();

MaterialApp(
  navigatorKey: navigatorKey,
  home: const HomePage(),
);

// anywhere in the app
navigatorKey.currentState?.pushNamed('/details');

3Verify

Trigger the navigation from the service and check that the route opens.

flutter run

Confirmed by api.flutter.dev

Did this fix it?

How do I stop it coming back?

  • Put each screen in its own widget class so its context is always below MaterialApp.
  • Keep one navigatorKey in a single file when non-widget code must navigate.
  • Do not change the navigatorKey at runtime: a new key builds a new Navigator and loses the app state.

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.