Sub topic 2
Setting up GoRouter with MaterialApp.router
This chapter assumes that you know Dart null safety, can build Flutter widgets, and have used Navigator.push and Navigator.pop. It also assumes basic knowledge of ChangeNotifier, although authentication state is covered later.
Why use GoRouter?
Flutter's original Navigator API is imperative. Imperative code tells Flutter what action to perform:
Navigator.of(context).push(
MaterialPageRoute<void>(
builder: (_) => const DetailsScreen(),
),
);This works well for small flows. As an app grows, you may need deep links, browser URLs, authentication rules, and nested navigation. Managing these features by manually pushing and removing pages becomes difficult.
go_router uses declarative routing. You declare which locations your app supports and which widget belongs to each location. The router then builds the page stack that matches the current location.
| Concept | Meaning |
|---|---|
| Location | A URL-style string such as `/books/42` |
| Route | A declaration that matches a location |
| `GoRouter` | The object that matches locations and creates navigation |
| `MaterialApp.router` | The app entry point that uses a declarative router |
Add the package and configure the app
Add go_router to pubspec.yaml, then run flutter pub get:
dependencies:
flutter:
sdk: flutter
go_router: ^15.1.2Create a GoRouter with a list of routes. Each GoRoute has a path and a builder. The builder receives the current BuildContext and router state.
import 'package:flutter/material.dart';
import 'package:go_router/go_router.dart';
final GoRouter router = GoRouter(
initialLocation: '/',
routes: <RouteBase>[
GoRoute(
path: '/',
name: 'home',
builder: (context, state) => const HomeScreen(),
),
GoRoute(
path: '/settings',
name: 'settings',
builder: (context, state) => const SettingsScreen(),
),
],
);
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({super.key});
@override
Widget build(BuildContext context) {
return MaterialApp.router(
title: 'Routing Demo',
routerConfig: router,
);
}
}
class HomeScreen extends StatelessWidget {
const HomeScreen({super.key});
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: const Text('Home')),
body: Center(
child: ElevatedButton(
onPressed: () => context.go('/settings'),
child: const Text('Open settings'),
),
),
);
}
}
class SettingsScreen extends StatelessWidget {
const SettingsScreen({super.key});
@override
Widget build(BuildContext context) {
return const Scaffold(
body: Center(child: Text('Settings')),
);
}
}initialLocation selects the first location when the app starts. The root route matches /, while the settings route matches /settings.
MaterialApp.router replaces MaterialApp's home, routes, and onGenerateRoute configuration. Pass the router through routerConfig; do not configure both routerConfig and the older individual router properties.
Visual model
Exercise
What is the role of routerConfig on MaterialApp.router?
Show answer
It supplies the GoRouter that matches the current location and builds the corresponding route stack.