Sub topic 13
Handling Links Manually with `app_links`
This chapter assumes you know basic Dart, Flutter widgets, URL parts, and Flutter navigation. It also assumes you can run an app on an Android emulator or iOS simulator. Earlier chapters covered verified Android App Links, iOS Universal Links, and go_router.
The app_links package gives your Dart code direct access to incoming links. You can then validate each link and send an accepted path to your router. This approach is useful when platform setup is already complete or when you need custom processing before navigation.
How manual link handling works
An app can receive a link in two main situations:
- A cold start happens when the operating system launches an app that is not running.
- A warm start happens when a running app receives another link.
The package exposes these situations through separate APIs:
getInitialLink()reads the link that launched the app.uriLinkStreamemits links received while the app is running.
The platform still decides whether your app receives the link. Android intent filters, iOS Universal Links, or a custom URL scheme must be configured separately.
Read initial and runtime links
Create one link coordinator near the application root. It reads the initial link once and subscribes to the stream once. Starting multiple listeners can cause duplicate navigation.
import 'dart:async';
import 'package:app_links/app_links.dart';
import 'package:go_router/go_router.dart';
class LinkCoordinator {
LinkCoordinator(this.router);
final GoRouter router;
final AppLinks _appLinks = AppLinks();
StreamSubscription<Uri>? _subscription;
Future<void> start() async {
final initialUri = await _appLinks.getInitialLink();
if (initialUri != null) {
_handle(initialUri);
}
_subscription = _appLinks.uriLinkStream.listen(_handle);
}
void _handle(Uri uri) {
if (uri.scheme != 'https' || uri.host != 'example.com') {
return;
}
if (!uri.path.startsWith('/products/')) {
return;
}
final target = uri.hasQuery
? '${uri.path}?${uri.query}'
: uri.path;
router.go(target);
}
Future<void> dispose() async {
await _subscription?.cancel();
}
}The coordinator accepts only HTTPS links from the expected host. It passes the path and query to go_router, rather than passing the complete external URL. The router can then match /products/:id and decode its parameters.
Start the coordinator after creating the router and before the app begins normal interaction. In a stateful root widget, call start() from initialization and dispose() when the widget is removed.
Common errors
- Handling only
uriLinkStreammisses a cold-start link. - Handling only
getInitialLink()misses links received while the app is open. - Creating a new subscription during every rebuild causes repeated navigation.
- Forwarding arbitrary hosts can route unexpected content.
- Calling
router.go(uri.toString())may expose an external scheme or host to route matching. Pass only the validated route portion.
Exercise
Why should a manual link handler read both getInitialLink() and uriLinkStream?
Show answer
getInitialLink() handles the link that launches a cold app, while uriLinkStream handles links received after the app is already running.