Sub topic 16
Deferred Deep Links and Install Attribution
Assume you already understand Flutter navigation, URI parts, and verified Android App Links or iOS Universal Links. This chapter covers a different case: a user taps a link before installing your app. After installation, the app must recover the intended destination.
What deferred deep linking means
A normal deep link opens an installed app immediately. A deferred deep link preserves the destination while the user moves through an install flow:
- The user taps a campaign link.
- The system opens the app store because the app is missing.
- The user installs and launches the app.
- An attribution service returns the original link.
- Flutter routes the user to the matching screen.
Install attribution records where an install came from. It may include campaign data, such as ref, and a deferred destination, such as /products/42.
| Link state | Result |
|---|---|
| App already installed | Open the destination immediately |
| App not installed | Store attribution and restore the destination after first launch |
The operating system does not provide a universal, automatic handoff for every store and platform. Deferred links usually require a provider SDK or a backend that coordinates the click, install, and first launch.
The handoff model
Keep the attribution provider separate from your router. The provider reports data. Your application validates that data and converts it into an internal route.
A provider may return the value only on the first launch, or it may return it through a stream. Read the provider documentation for its exact initialization order and timing. Initialize it before deciding which startup screen to show.
Validate before routing
Treat attribution data as untrusted input. Do not route to an arbitrary URL, execute commands from query parameters, or trust campaign fields as authorization.
The following small adapter accepts only your HTTPS host and approved paths:
Uri? safeDeferredUri(String? raw) {
if (raw == null || raw.isEmpty) return null;
final uri = Uri.tryParse(raw);
if (uri == null ||
uri.scheme != 'https' ||
uri.host != 'example.com' ||
uri.fragment.isNotEmpty) {
return null;
}
final segments = uri.pathSegments;
if (segments.length == 2 && segments.first == 'products') {
final id = segments[1];
if (RegExp(r'^[A-Za-z0-9_-]+$').hasMatch(id)) {
return Uri(path: '/products/$id');
}
}
return null;
}
void handleFirstLaunch(String? rawLink) {
final route = safeDeferredUri(rawLink);
if (route == null) {
// Show the normal home screen.
return;
}
// Pass route.toString() to your router after it is ready.
print('Open ${route.path}');
}This adapter removes the original host and query parameters after validation. Preserve approved campaign parameters separately if analytics needs them. Do not use an unvalidated redirect query parameter as a navigation target.
First-launch decisions
Your startup code needs to distinguish three results:
- No attribution data: open the normal home screen.
- Valid deferred data: wait until the router is ready, then navigate once.
- Invalid or delayed data: use a short loading state, then fall back safely.
Prevent duplicate navigation when both the initial-link callback and a link stream deliver the same URI. Store a processed identifier or normalized route during that startup session.
Exercise
Why should a deferred link be validated before it reaches go_router?
Show answer
The link comes from outside the app and can contain an unexpected host, path, or parameter. Validation limits navigation to approved destinations and prevents unsafe redirects.