Sub topic 6
Navigating by route name
This chapter assumes that you know Dart null safety, can build Flutter widgets, and understand GoRouter, GoRoute, and MaterialApp.router. It also assumes that you have used basic Navigator.push and Navigator.pop calls.
A route name gives a stable identifier to a route. Code can navigate using that identifier instead of repeating the route's URL path. This is useful when a path changes but the meaning of the destination stays the same.
Defining a named route
Assign a name when you declare a GoRoute. The name must be unique within the router.
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: '/books/:id',
name: 'book',
builder: (context, state) {
final String id = state.pathParameters['id']!;
return BookScreen(id: id);
},
),
],
);
void main() {
runApp(
MaterialApp.router(
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.goNamed(
'book',
pathParameters: <String, String>{'id': '42'},
);
},
child: const Text('Open book 42'),
),
),
);
}
}
class BookScreen extends StatelessWidget {
const BookScreen({super.key, required this.id});
final String id;
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: Text('Book $id')),
body: const Center(child: Text('Book details')),
);
}
}goNamed looks up the route by its name and creates the location. The pathParameters map supplies values for placeholders such as :id. The resulting location is /books/42.
Adding query parameters
Query parameters are optional values after a question mark. Pass them with queryParameters.
context.goNamed(
'book',
pathParameters: <String, String>{'id': '42'},
queryParameters: <String, String>{'ref': 'home'},
);This produces /books/42?ref=home. Read the value from state.uri.queryParameters:
final String? source = state.uri.queryParameters['ref'];Path parameters identify the resource. Query parameters usually modify how the resource is displayed, filtered, or reached.
Choosing named navigation
| Navigation style | Best use |
|---|---|
| `context.go('/books/42')` | A fixed, directly readable location |
| `context.goNamed('book', pathParameters: ...)` | A destination whose path may change |
| `context.pushNamed('book', pathParameters: ...)` | A named destination that should be added above the current page |
goNamed follows the same stack-rebuilding behavior as go. pushNamed follows the same stack behavior as push. Choose based on navigation behavior first, then choose the named or path-based form.
Exercise
A route is named profile and has the path /users/:id. How do you navigate to user 7 while adding the destination to the current stack?
Show answer
Use context.pushNamed('profile', pathParameters: <String, String>{'id': '7'});.