Sub topic 9
Handling Deep Links on Cold Start and While the App Is Running
Assume you know Dart, Flutter navigation, go_router, and the Android App Links or iOS Universal Links setup from earlier chapters. This chapter focuses on when Flutter receives a link and how to avoid processing the same link twice.
A deep link can arrive in two different process states:
| State | What happens | Main concern |
|---|---|---|
| Cold start | The operating system launches the app with a URL | Read the initial URL before routing |
| Warm start | The app is already running or in the background | Listen for a new URL event |
The router should become the single owner of navigation. Link-handling code should validate the URL, convert it to an in-app location, and give that location to the router.
Cold start: process the initial URI
On a cold start, the operating system includes the URL in the launch request. Your startup code must read that initial URI once. If you use a router with Flutter's platform deep-link integration, MaterialApp.router can provide the initial route to the router. Do not also navigate to the same location manually unless you have disabled the automatic integration.
When handling links with app_links, keep initial-link processing and stream subscription in one coordinator. The exact method used to read the initial link depends on the package version, so check that version's API. The coordinator below accepts the initial URI after the package has read it:
import 'dart:async';
import 'package:app_links/app_links.dart';
import 'package:go_router/go_router.dart';
class DeepLinkCoordinator {
DeepLinkCoordinator(this.router, {AppLinks? links})
: _links = links ?? AppLinks();
final GoRouter router;
final AppLinks _links;
StreamSubscription<Uri>? _subscription;
void start({Uri? initialUri}) {
if (initialUri != null) {
_handle(initialUri);
}
_subscription = _links.uriLinkStream.listen(_handle);
}
void _handle(Uri uri) {
if (uri.scheme != 'https' || uri.host != 'example.com') {
return;
}
final location = uri.hasQuery ? '${uri.path}?${uri.query}' : uri.path;
router.go(location);
}
Future<void> dispose() async {
await _subscription?.cancel();
}
}Call start once, after creating the router. Pass the package's initial URI as initialUri. The stream handles later links while the application is running.
Warm start: listen for link events
A warm-start event can occur while the app is visible or after it returns from the background. The operating system sends the URL to the running process, and uriLinkStream emits it.
Avoid creating a new stream listener every time a screen rebuilds. Start the coordinator from an application-level object, such as the app state or a service initialized in main. Cancel the subscription when that object is disposed.
Common startup errors
- Registering the stream listener inside
build, which creates duplicate listeners. - Routing before authentication state has loaded, causing a brief wrong screen.
- Treating a warm-start link as a new application session.
- Accepting any scheme or host without validation.
- Losing query parameters when converting a
Urito a router location.
Exercise
Why must cold-start and warm-start links use different handling paths?
Show answer
A cold-start link is available during application initialization, while a warm-start link arrives later through a stream event. The application must read the first URI once and subscribe for later URIs.