Sub topic 12
Path and Query Parameters in Flutter
Assume you know Dart async and await, basic Flutter widgets, BuildContext, and the go_router setup from the previous chapter. This chapter uses go_router to read values from a URL-like location.
Two kinds of route parameters
A path parameter is a variable part of a path. It usually identifies a specific resource. In /products/42, 42 is the product ID.
A query parameter follows a question mark. It usually changes how a resource is displayed. In /products/42?sort=price, sort=price is a query parameter.
| Parameter | Example | Common use |
|---|---|---|
| Path | `/products/42` | Required identity, such as an ID |
| Query | `?sort=price&tag=flutter` | Optional filters, sorting, or search text |
The route pattern declares path parameters with a colon. Query parameters do not appear in the pattern. go_router matches the path, then exposes both kinds of values through GoRouterState.
Configure and read parameters
Add go_router to pubspec.yaml, then define a route with :id. Read the path value from state.pathParameters. Read query values from state.uri.queryParameters.
import 'package:flutter/material.dart';
import 'package:go_router/go_router.dart';
final router = GoRouter(
initialLocation: '/',
routes: [
GoRoute(
path: '/',
builder: (context, state) => Scaffold(
appBar: AppBar(title: const Text('Catalog')),
body: Center(
child: ElevatedButton(
onPressed: () {
final location = Uri(
path: '/products/42',
queryParameters: {
'sort': 'price',
'tag': 'flutter',
},
).toString();
context.go(location);
},
child: const Text('Open product'),
),
),
),
),
GoRoute(
path: '/products/:id',
builder: (context, state) {
final id = state.pathParameters['id']!;
final sort = state.uri.queryParameters['sort'] ?? 'relevance';
final tags = state.uri.queryParametersAll['tag'] ?? const <String>[];
return ProductScreen(id: id, sort: sort, tags: tags);
},
),
],
);
class ProductScreen extends StatelessWidget {
const ProductScreen({
required this.id,
required this.sort,
required this.tags,
super.key,
});
final String id;
final String sort;
final List<String> tags;
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: Text('Product $id')),
body: Text('Sort: $sort\nTags: ${tags.join(', ')}'),
);
}
}
class MyApp extends StatelessWidget {
const MyApp({super.key});
@override
Widget build(BuildContext context) {
return MaterialApp.router(routerConfig: router);
}
}
void main() => runApp(const MyApp());Uri builds and encodes the location safely. This is preferable to concatenating user input into a string. A missing optional query value should receive a useful default. A missing required path value indicates a route or application error, so the example uses ! only after the route pattern guarantees id.
Common mistakes
- Do not declare
path: '/products/:id?sort=:sort'. Query parameters belong after the path and are read fromstate.uri. - Do not assume every query parameter exists. Use
??or validate the value. - Use
queryParametersAllwhen a key can occur more than once, such as?tag=flutter&tag=dart. - Treat path values as strings until you validate and convert them. For example, use
int.tryParse(id)when the ID must be numeric. - Choose
context.gowhen the location should replace the current navigation state. Choosecontext.pushwhen the user should be able to return with the back button.
Exercise
For the location /search?term=flutter&page=2, which part is the path and which values should you read from state.uri.queryParameters?
Show answer
The path is /search. Read term as flutter and page as 2; both query values are strings and should be validated before numeric conversion.