Sub topic 6
Path and Query Parameters in Flutter Deep Links
This chapter assumes basic Dart and Flutter knowledge, familiarity with go_router, and an understanding of URL parts. You will use a path parameter when a value identifies the resource being opened. You will use a query parameter when a value modifies how that resource is displayed.
Path parameters identify a resource
A path parameter is a named placeholder inside a route path. For example:
/products/42Here, 42 is the product identifier. The route pattern uses :id:
/products/:idgo_router places matched path parameters in state.pathParameters.
import 'package:flutter/material.dart';
import 'package:go_router/go_router.dart';
void main() {
runApp(MaterialApp.router(routerConfig: router));
}
final router = GoRouter(
routes: [
GoRoute(
path: '/',
builder: (context, state) => const Text('Home'),
),
GoRoute(
path: '/products/:id',
builder: (context, state) {
final id = state.pathParameters['id']!;
return ProductScreen(id: id);
},
),
],
);
class ProductScreen extends StatelessWidget {
const ProductScreen({required this.id, super.key});
final String id;
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: Text('Product $id')),
body: Center(child: Text('Loading product $id')),
);
}
}The ! asserts that id exists. The route pattern requires it, but input still crosses a platform boundary. For important data, validate the value before using it:
final rawId = state.pathParameters['id'];
final id = int.tryParse(rawId ?? '');
if (id == null || id <= 0) {
return const Text('Invalid product ID');
}Query parameters modify a route
A query parameter follows a ? and uses key-value pairs:
/products/42?ref=email&view=compactThe path identifies product 42. The query parameters provide the referral source and display preference. Read them through state.uri.queryParameters.
GoRoute(
path: '/products/:id',
builder: (context, state) {
final id = state.pathParameters['id']!;
final ref = state.uri.queryParameters['ref'];
final compact = state.uri.queryParameters['view'] == 'compact';
return ProductScreen(
id: id,
referral: ref,
compact: compact,
);
},
)A missing query parameter returns null. Do not assume that a value exists or has the expected format. Query values can also be repeated, such as ?tag=flutter&tag=dart. Use queryParametersAll when repeated values matter.
| URL part | Example | Typical purpose |
|---|---|---|
| Path parameter | `/products/42` | Select a required resource |
| Query parameter | `?ref=email` | Apply optional context or filters |
Encoding and route boundaries
Use Uri to construct links rather than joining strings manually. It encodes spaces and reserved characters correctly.
final uri = Uri(
scheme: 'https',
host: 'example.com',
pathSegments: ['search'],
queryParameters: {'q': 'flutter deep links'},
);
print(uri); // https://example.com/search?q=flutter+deep+linksHow values reach a screen
Exercise
Which part of /orders/abc123?filter=open identifies the order, and which part changes the displayed results?
Show answer
abc123 is the path parameter that identifies the order. filter=open is the query parameter that changes the displayed results.