Sub topic 3
Navigator keys and the root navigator
This chapter assumes you know Dart and Flutter widgets, understand nested Navigator objects, and have used GoRouter, GoRoute, and context.go. The goal is to identify which navigator owns a route and to place a route at the correct level.
Why navigator keys matter
A Navigator owns a stack of pages. In a tabbed application, there is usually:
- One root navigator for the whole app.
- One navigator for each
StatefulShellBranch. - Sometimes additional navigators created by nested shell routes.
A navigator key is a stable identifier for one of these navigators. You use the key when a route must explicitly target a particular navigator.
Without explicit keys, it is easy to place a page inside a tab when it should cover the entire app, or to place a page above the wrong shell.
| Key | Usually belongs to | Typical purpose |
|---|---|---|
| `rootNavigatorKey` | The `GoRouter` | Display pages above the shell |
| `homeNavigatorKey` | The Home branch | Keep Home's stack independent |
| `settingsNavigatorKey` | The Settings branch | Keep Settings' stack independent |
Creating the root and branch keys
Create the root key once and pass it to GoRouter. Branch keys belong to their StatefulShellBranch objects.
import 'package:flutter/material.dart';
import 'package:go_router/go_router.dart';
final rootNavigatorKey = GlobalKey<NavigatorState>();
final homeNavigatorKey = GlobalKey<NavigatorState>();
final settingsNavigatorKey = GlobalKey<NavigatorState>();
final router = GoRouter(
navigatorKey: rootNavigatorKey,
initialLocation: '/home',
routes: [
StatefulShellRoute.indexedStack(
builder: (context, state, navigationShell) {
return Scaffold(body: navigationShell);
},
branches: [
StatefulShellBranch(
navigatorKey: homeNavigatorKey,
routes: [
GoRoute(
path: '/home',
builder: (context, state) => const HomeScreen(),
),
],
),
StatefulShellBranch(
navigatorKey: settingsNavigatorKey,
routes: [
GoRoute(
path: '/settings',
builder: (context, state) => const SettingsScreen(),
),
],
),
],
),
],
);The root key is attached to the router. Each branch key identifies the navigator that owns that branch's route subtree. Giving branches explicit keys is useful when you need to reason about ownership or refer to a navigator directly.
Selecting the root navigator with `parentNavigatorKey`
A route normally uses the nearest navigator in its route tree. To display a route above the shell, set its parentNavigatorKey to rootNavigatorKey.
StatefulShellBranch(
navigatorKey: homeNavigatorKey,
routes: [
GoRoute(
path: '/home',
builder: (context, state) => const HomeScreen(),
routes: [
GoRoute(
path: 'details/:id',
builder: (context, state) {
return DetailsScreen(
id: state.pathParameters['id']!,
);
},
),
GoRoute(
path: 'photo',
parentNavigatorKey: rootNavigatorKey,
builder: (context, state) => const PhotoViewerScreen(),
),
],
),
],
)details/:id uses the Home branch navigator. The tab bar remains visible because the details page is inside the branch stack.
photo targets the root navigator. It is still declared under Home for route-structure purposes, but it is presented above the shell. This is useful for photo viewers, login pages, checkout flows, and other full-screen experiences.
>callout note > parentNavigatorKey must refer to an ancestor navigator key. A branch route can target the root navigator, but an unrelated navigator key will cause a route configuration error.
Common mistakes
- Defining a root key but forgetting
navigatorKey: rootNavigatorKeyonGoRouter. - Using the root key for every branch, which removes branch ownership and can break state preservation.
- Expecting a nested route to cover the shell without setting
parentNavigatorKey. - Creating keys inside
build. Keys should be long-lived, top-level values or stable fields. - Confusing a navigator key with a route path.
/homeidentifies a location;homeNavigatorKeyidentifies the navigator that manages it.
Exercise
A details page should keep the tab bar visible, but a photo viewer should cover it. Which navigator should own each page?
Show answer
The details page should use the branch navigator by omitting parentNavigatorKey. The photo viewer should use the root navigator by setting parentNavigatorKey: rootNavigatorKey.