Sub topic 4
How Flutter receives a link from the platform
This chapter follows one link from the moment the operating system hands it to your app until a route receives it. It assumes you know the URI parts from the previous chapters (scheme, host, path, query), that you have built a Flutter app for Android or iOS, and that you have used Navigator with named routes. You don't need the Android or iOS verification setup yet. That comes in later chapters.
The two halves of delivery
A link never reaches your Dart code directly. Two separate layers handle it.
- The native layer (Android
Activity, iOSAppDelegate) receives the link from the operating system. Which native entry point fires depends on the platform and on whether the app was already running. - The Flutter engine forwards the link to the framework over the
flutter/navigationplatform channel. The framework passes it to theRouter, or to theNavigatorin apps that don't useMaterialApp.router.
You normally write no native code for this. Flutter's embedding already handles both halves. Knowing how it works helps when links arrive twice, never arrive, or open the wrong screen.
Native entry points
| Situation | Android | iOS |
|---|---|---|
| App not running (cold start) | The launch `Intent` carries the URI in `intent.data`. | `application(_:didFinishLaunchingWithOptions:)` receives the URL or user activity in the launch options. |
| App already running | `onNewIntent` receives the new `Intent`. | `application(_:open:options:)` for custom schemes, `application(_:continue:restorationHandler:)` for Universal Links. |
Flutter's FlutterActivity and FlutterAppDelegate already implement these methods. Each one extracts the URI and sends it to Dart. On iOS, FlutterAppDelegate first offers the link to any registered plugin. If a plugin claims it, the Flutter framework does not see it from that path.
The following diagram shows the path on Android. The iOS path has the same shape but starts in the app delegate.
Cold start: the initial route
On a cold start, the Dart side may not exist yet when the link arrives. The engine can't send a message to a framework that hasn't started. Instead, the embedding records the link as the initial route before it runs main. Dart exposes it as PlatformDispatcher.instance.defaultRouteName. Without a link, this value is /.
MaterialApp.router reads this value when the app builds and uses it as the first location to resolve. go_router does the same. If the platform supplies a link other than /, it takes priority over the initialLocation you configured. That is why a cold-start link opens the product screen directly, without passing through the home screen first.
Warm start: pushed route information
When the app is already running, the engine sends a pushRouteInformation message over the navigation channel. WidgetsBinding receives it and calls didPushRouteInformation on its observers in registration order. The first observer that returns true stops the chain. The router's platform provider, PlatformRouteInformationProvider, is one of these observers. It parses the URI and tells the Router to navigate.
You can add your own observer to see what arrives. Return false so the router still handles the link.
import 'package:flutter/material.dart';
import 'package:go_router/go_router.dart';
class LinkLogger with WidgetsBindingObserver {
void start() => WidgetsBinding.instance.addObserver(this);
@override
Future<bool> didPushRouteInformation(RouteInformation info) async {
debugPrint('Warm link: ${info.uri}');
return false; // let the router handle it too
}
}
void main() {
WidgetsFlutterBinding.ensureInitialized();
debugPrint('Cold link: ${WidgetsBinding.instance.platformDispatcher.defaultRouteName}');
LinkLogger().start();
runApp(const MyApp());
}
final _router = GoRouter(
routes: [
GoRoute(path: '/', builder: (c, s) => const Scaffold(body: Text('Home'))),
GoRoute(
path: '/products/:id',
builder: (c, s) => Scaffold(body: Text('Product ${s.pathParameters['id']}')),
),
],
);
class MyApp extends StatelessWidget {
const MyApp({super.key});
@override
Widget build(BuildContext context) => MaterialApp.router(routerConfig: _router);
}Run this and open a link while the app is in the foreground. The "Warm link" line prints, and the router switches to the product screen. Launch the app from a stopped state and the "Cold link" line prints instead. The observer is not called for the cold-start link, because that link was already consumed as the initial route.
Built-in handling versus manual handling
Flutter's built-in handling is on by default in current Flutter releases. In older releases you had to opt in. You control it with one flag per platform.
<!-- android/app/src/main/AndroidManifest.xml, inside <activity> -->
<meta-data android:name="flutter_deeplinking_enabled" android:value="false" />
<!-- ios/Runner/Info.plist -->
<key>FlutterDeepLinkingEnabled</key>
<false/>Setting the flag to false stops Flutter from forwarding links to the framework. Do this only when a plugin such as app_links or an analytics SDK takes over link handling. If you leave both enabled, the same link reaches your router twice: once through the built-in channel and once through the plugin's stream. Symptoms are the destination screen appearing twice on the stack, or a login redirect firing twice. Pick one handler per link.
Android: launch mode matters
The default Flutter Android template sets android:launchMode="singleTop" on MainActivity. With this mode, a link that arrives while the activity is on top is delivered through onNewIntent to the existing activity. If you change the mode to standard, each link can start a new activity, and with it a new Flutter engine and a second copy of your app state. Keep singleTop (or singleTask) unless you have a specific reason.
iOS: custom schemes and Universal Links take different paths
On iOS, a custom scheme link such as myapp://products/42 arrives through application(_:open:options:). A Universal Link arrives through application(_:continue:restorationHandler:) as an NSUserActivity of type NSUserActivityTypeBrowsingWeb. The framework ends up with a URI in both cases. The difference matters only when you write native code, or when a plugin must be registered to see one of the two paths.
Common errors
- Link opens the app but lands on the home screen. The route did not match, or a redirect sent the user elsewhere. Print the incoming URI using the logger above to check that the link reached Dart at all.
- Link never reaches Dart. Either built-in handling is disabled, or a native plugin returned
truefrom its delegate method and claimed the link first. - Link handled twice. Built-in handling and a plugin are both active. Disable one.
- Cold start works, warm start does not (or the reverse). Check
launchModeon Android, and check that your own code is not registering the observer only after the first frame.
Exercise
A user taps https://example.com/products/42 while your app is closed. Which value in Dart holds the link, and why is didPushRouteInformation not called for it?
Show answer
The link is stored as the initial route and read through PlatformDispatcher.instance.defaultRouteName. The engine can't send a message to a framework that has not started, so the embedding records the link before main runs. MaterialApp.router uses that value as its first location. didPushRouteInformation is only called for links that arrive while the app is running.
Exercise
After adding the app_links package, tapping a link opens the product screen twice on the navigation stack. What is the likely cause, and what are two ways to fix it?
Show answer
Both Flutter's built-in link handling and the plugin deliver the same link, so the router handles it twice. Fix it by disabling built-in handling (flutter_deeplinking_enabled set to false in AndroidManifest.xml and FlutterDeepLinkingEnabled set to false in Info.plist) and routing links only from the plugin's stream. Or remove the plugin listener and rely on the built-in handling.