Flutter
URL parameters
Read path parameters and query strings.
Path parameters
In a GoRoute path, names starting with a colon like :id define a path parameter. You access its value through the pathParameters property of the state object passed to the builder.
GoRoute(
path: '/kullanici/:id',
builder: (context, state) {
final id = state.pathParameters['id']!;
return KullaniciScreen(id: id);
},
)
Query parameters
You access query strings in the form ?key=value in a URL through state.queryParameters. For example, in the path /ara?kelime=elma, state.queryParameters['kelime'] returns 'elma'.
GoRoute(
path: '/ara',
builder: (context, state) {
final kelime = state.queryParameters['kelime'] ?? '';
return AraScreen(kelime: kelime);
},
)
Exercise Screen with a path parameter
Define a GoRoute with the path /kullanici/:id. In the builder, read state.pathParameters['id'] and return KullaniciScreen(id: id). Print 'hazir' in main.
Show example solution
import 'package:flutter/widgets.dart';
import 'package:go_router/go_router.dart';
class KullaniciScreen extends StatelessWidget {
final String id;
const KullaniciScreen({super.key, required this.id});
@override
Widget build(BuildContext context) => const SizedBox.shrink();
}
final route = GoRoute(
path: '/kullanici/:id',
builder: (context, state) {
final id = state.pathParameters['id']!;
return KullaniciScreen(id: id);
},
);
void main() {
print('hazir');
}
Test yourself
-
What does ':id' in a GoRoute path specify?
- A constant value
- A widget name
- A query parameter
- A path parameter
Show answer
A path parameter — Names starting with a colon are path parameters; the value at that position in the URL is read via state.pathParameters. It is not a constant, not a widget name, and a query parameter starts with '?'.
-
Read the path parameter
GoRoute(
path: '/kullanici/:id',
builder: (context, state) {
final id = state.____!;
return KullaniciScreen(id: id);
},
)
Show answer
GoRoute(
path: '/kullanici/:id',
builder: (context, state) {
final id = state.pathParameters['id']!;
return KullaniciScreen(id: id);
},
)
-
Parameter parts
- :id
- Path parameter definition
- state.pathParameters
- Path parameters
- state.queryParameters
- Query parameters
- ?kelime=elma
- Query string
Next lesson: Summary - GoRouter