Sub topic 5
Path and Query Parameters
Assume you know Dart null safety, can build Flutter widgets, and have used Navigator.push and Navigator.pop. This chapter shows how a go_router location carries data into a screen.
A route can read two common kinds of URL data:
- A path parameter is a required value inside the path, such as
42in/books/42. - A query parameter is an optional key-value pair after
?, such astab=reviewsin/books/42?tab=reviews.
Use a path parameter to identify the resource being opened. Use query parameters for optional view state, filters, sorting, or search terms.
Reading parameters from a route
Declare a path parameter with a colon:
GoRoute(
path: '/books/:id',
builder: (context, state) {
final String id = state.pathParameters['id']!;
final String tab =
state.uri.queryParameters['tab'] ?? 'overview';
return BookScreen(id: id, tab: tab);
},
)The id value comes from the path. The tab value comes from the query string. The ! is appropriate here because the route pattern requires :id. For optional values, provide a default or handle null.
Query parameters are not separately declared in the route pattern. Any query key can be read from state.uri.queryParameters.
A complete example
The following app opens a book using a path parameter and selects a tab using a query parameter.
import 'package:flutter/material.dart';
import 'package:go_router/go_router.dart';
final GoRouter router = GoRouter(
initialLocation: '/',
routes: <RouteBase>[
GoRoute(
path: '/',
builder: (context, state) => const HomeScreen(),
),
GoRoute(
path: '/books/:id',
builder: (context, state) {
final String id = state.pathParameters['id']!;
final String tab =
state.uri.queryParameters['tab'] ?? 'overview';
return BookScreen(id: id, tab: tab);
},
),
],
);
void main() {
runApp(const MaterialApp.router(routerConfig: router));
}
class HomeScreen extends StatelessWidget {
const HomeScreen({super.key});
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: const Text('Library')),
body: Center(
child: ElevatedButton(
onPressed: () => context.go('/books/42?tab=reviews'),
child: const Text('Open reviews'),
),
),
);
}
}
class BookScreen extends StatelessWidget {
const BookScreen({
super.key,
required this.id,
required this.tab,
});
final String id;
final String tab;
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: Text('Book $id')),
body: Center(child: Text('Selected tab: $tab')),
);
}
}The URL is meaningful on its own. A browser can open /books/42?tab=reviews directly, and the router can reconstruct the same screen state.
Encoding and choosing the right parameter
When constructing URLs from user input, encode values so spaces and reserved characters do not break the URL. Uri can build a query safely:
final Uri location = Uri(
path: '/books/42',
queryParameters: {'search': 'Dart & Flutter'},
);
context.go(location.toString());Keep stable resource identity in the path. Keep temporary display choices in the query string.
| Data | Example | Suitable location |
|---|---|---|
| Book identity | `42` | `/books/:id` |
| Selected tab | `reviews` | `?tab=reviews` |
| Search filter | `flutter` | `?search=flutter` |
Exercise
What values does the route /books/7?tab=details&sort=recent provide for id and tab?
Show answer
id is 7, read from state.pathParameters['id'], and tab is details, read from state.uri.queryParameters['tab']. The sort value is also available as a query parameter.