Sub topic 2
Declarative Routing in Flutter with go_router
This chapter assumes you know how to attach a GoRouter to MaterialApp.router, define flat GoRoutes with paths, and use Navigator.push and pop. You should also be comfortable with ChangeNotifier.
Navigating by route name
Give a route a name, then navigate with goNamed. The name stays fixed even if the path changes, so only the route definition needs editing.
import 'package:flutter/material.dart';
import 'package:go_router/go_router.dart';
final GoRouter router = GoRouter(
routes: <RouteBase>[
GoRoute(
path: '/',
name: 'home',
builder: (context, state) => const Text('Home'),
routes: <RouteBase>[
GoRoute(
path: 'books/:id',
name: 'book',
builder: (context, state) =>
Text('Book ${state.pathParameters['id']}'),
),
],
),
],
);
// Elsewhere in a widget:
// context.goNamed('book', pathParameters: {'id': '42'});Nested routes and the back stack
Child routes sit inside a parent's routes: list. Their paths omit the leading slash. Navigating to a child with go builds the parent beneath it, so pop returns to the parent. push instead adds the page on top of whatever stack already exists.
| Call | Effect on the stack |
|---|---|
| `go('/books/42')` | Replaces the stack with home, then book 42 |
| `push('/books/42')` | Adds book 42 on top of the current stack |
Passing objects with extra and its limits
extra carries any Dart object between routes. The receiving route reads it from state.extra and must cast it.
extra lives only in memory. A browser refresh, a restored app, or a deep link drops it, so the receiving screen gets null. Put identifiers in path or query parameters for data that must survive. Use extra for large or non-serializable objects you can afford to lose.
Redirects and authentication guards
A redirect callback runs before a route builds. Return a path to send the user elsewhere, or null to allow the navigation.
final GoRouter router = GoRouter(
initialLocation: '/',
redirect: (context, state) {
final bool loggedIn = auth.loggedIn;
final bool atLogin = state.matchedLocation == '/login';
if (!loggedIn) return atLogin ? null : '/login';
if (atLogin) return '/';
return null;
},
routes: <RouteBase>[
GoRoute(path: '/', builder: (context, state) => const Text('Home')),
GoRoute(path: '/login', builder: (context, state) => const Text('Login')),
],
);Re-evaluating redirects with refreshListenable
redirect runs when a navigation happens. It does not run when your auth state changes. refreshListenable fixes that: when the listenable notifies, the router reruns redirect and moves the user if needed.
flowchart TD
A[AuthState calls notifyListeners] --> B[Router reruns redirect]
B --> C{Logged in?}
C -- No --> D[Send user to /login]
C -- Yes --> E[Keep current route]Exercise
Exercise
A user is on /books/42 when they log out. Which line of the router setup makes go_router send them to /login without any navigation call?
Show answer
Passing the AuthState to refreshListenable. Its notifyListeners call makes the router rerun redirect, which returns '/login' when loggedIn is false.