Sub topic 15
Deep Linking and URLs
What deep linking means
Prerequisites: you should know Dart async and await, basic Flutter widgets, BuildContext, and the basics of go_router.
A deep link is a URL that opens a specific location inside an app. For example:
https://shop.example/products/42?tab=reviews
This link identifies product 42 and requests its reviews tab. A user can open it from a browser, email, message, or another app.
A Flutter router receives the URL, matches it against the route configuration, and builds the matching screen. The same process handles an initial URL when the app starts and a new URL while the app is already running.
Matching path and query parameters
A path parameter identifies a resource. In /products/42, 42 is the value for :id. A query parameter adds optional information after ?, such as tab=reviews.
With go_router, path values are available through state.pathParameters. Query values are available through state.uri.queryParameters.
import 'package:flutter/material.dart';
import 'package:go_router/go_router.dart';
void main() {
runApp(const ShopApp());
}
final router = GoRouter(
initialLocation: '/',
routes: [
GoRoute(
path: '/',
builder: (context, state) => HomeScreen(),
),
GoRoute(
path: '/products/:id',
builder: (context, state) {
final id = state.pathParameters['id']!;
final tab = state.uri.queryParameters['tab'] ?? 'overview';
return ProductScreen(id: id, tab: tab);
},
),
],
);
class ShopApp extends StatelessWidget {
const ShopApp({super.key});
@override
Widget build(BuildContext context) {
return MaterialApp.router(routerConfig: router);
}
}
class HomeScreen extends StatelessWidget {
HomeScreen({super.key});
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: const Text('Shop')),
body: Center(
child: ElevatedButton(
onPressed: () => context.go('/products/42?tab=reviews'),
child: const Text('Open product'),
),
),
);
}
}
class ProductScreen extends StatelessWidget {
const ProductScreen({
required this.id,
required this.tab,
super.key,
});
final String id;
final String tab;
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: Text('Product $id')),
body: Center(child: Text('Showing $tab')),
);
}
}The exclamation mark after pathParameters['id'] asserts that the value exists. Use it only when the route guarantees the parameter. Otherwise, validate the value and show an error screen or redirect.
Platform setup and failure cases
The router configuration handles matching, but the operating system must also send links to the app. Android uses intent filters. iOS uses Universal Links or custom URL schemes. Web hosting must serve the Flutter application for unknown paths instead of returning a server 404.
Test these cases:
- The app is closed when the link opens.
- The app is already running.
- The path contains an unknown product ID.
- Query parameters are missing or malformed.
- An unauthenticated user opens a protected link.
A valid deep link should produce the same screen whether navigation began inside the app or outside it.
Exercise
In /products/42?tab=reviews, which part is the path parameter and which part is the query parameter?
Show answer
42 is the value of the :id path parameter. reviews is the value of the tab query parameter.