Sub topic 4
How Flutter Receives a Link from the Platform
Assume you know basic Dart, Flutter widgets, and navigation. You should also understand URI parts such as the scheme, host, path, and query. This chapter focuses on delivery: how a link opened outside your app becomes input that Flutter can route.
The delivery path
A deep link starts outside Flutter. A user might tap an email link, select a browser suggestion, or run a platform command. The operating system first decides which application can handle the URI.
For a verified HTTPS link, Android or iOS checks the app's domain association. For a custom scheme such as myapp://products/42, the operating system finds an application that registered that scheme.
After selecting your application, the platform gives the URI to the application entry point:
- Android delivers it through an
Intentto an activity. - iOS delivers it through a universal-link activity or scene callback.
- Flutter's engine and embedding convert that platform event into information available to Dart.
- A router or link package turns the URI into navigation.
Flutter's default deep-link integration can provide an initial route when the application starts and route events while it is running. A declarative router such as go_router consumes those events through MaterialApp.router.
Initial links and runtime links
An initial link is the URI that caused a new application process to start. Your Dart code must read or receive it during startup. A runtime link arrives after the application is already running, often when the user taps another link.
These cases have different timing:
| Application state | Platform event | Handling concern |
|---|---|---|
| Cold start | URI starts the process | Initialize routing before displaying the target screen |
| Warm start | URI reaches an existing process | Update the current route without creating duplicate navigation |
| Background | URI resumes a paused process | Reuse runtime-link handling and refresh required data |
When using a package such as app_links, the initial URI and the stream of later URIs are separate operations. Register the stream listener early so a link is not missed.
import 'package:app_links/app_links.dart';
import 'package:go_router/go_router.dart';
final appLinks = AppLinks();
Future<void> connectLinkHandling(GoRouter router) async {
final initialUri = await appLinks.getInitialLink();
if (initialUri != null) {
_openUri(router, initialUri);
}
appLinks.uriLinkStream.listen((uri) {
_openUri(router, uri);
});
}
void _openUri(GoRouter router, Uri uri) {
if (uri.host != 'example.com') return;
final target = uri.hasQuery ? '${uri.path}?${uri.query}' : uri.path;
router.go(target);
}The exact startup API depends on the package version. Do not assume that a runtime stream also contains the cold-start URI. Check both paths.
Avoiding duplicate delivery
Choose one main delivery strategy. If go_router handles platform deep links automatically, do not also call router.go for the same event from app_links. Otherwise, one tap can produce two navigation events.
If you handle links manually, keep the handler small:
- Accept only expected schemes and hosts.
- Convert the URI into an internal route.
- Let the router handle navigation and unknown paths.
- Log failures without recording sensitive query values.
Exercise
Why must an app handle the initial URI separately from the runtime URI stream?
Show answer
A cold-start link may be delivered before the runtime stream is subscribed, so startup code must read the initial URI explicitly.