Sub topic 3
Sending deep links through login
This chapter assumes you know Flutter widgets, go_router basics, and Dart's Uri class. You need an Android emulator, an iOS simulator, and a domain you control.
Sending deep links through login
A signed-out user who opens a product link should land on the login screen, then return to the original link. A go_router redirect can do this by storing the target in a from query parameter.
final router = GoRouter(
refreshListenable: auth,
redirect: (context, state) {
final goingToLogin = state.matchedLocation == '/login';
if (!auth.isLoggedIn && !goingToLogin) {
final from = Uri.encodeComponent(state.uri.toString());
return '/login?from=$from';
}
return null;
},
routes: [
GoRoute(path: '/login', builder: (context, state) => const LoginScreen()),
// other routes
],
);After a successful login, read state.uri.queryParameters['from'], and navigate to it only if it starts with /.
Showing a fallback for unknown links
The errorBuilder in go_router runs when no route matches. It shows a screen instead of an error.
errorBuilder: (context, state) => NotFoundScreen(uri: state.uri),Testing deep links with adb and simctl
Test links from the command line so you do not need a browser or a message.
# Android
adb shell am start -a android.intent.action.VIEW \
-d "https://example.com/products/42" com.example.app
# iOS simulator
xcrun simctl openurl booted "https://example.com/products/42"Handling links manually with app_links
The app_links package gives you the incoming link as a stream. Use it when you must inspect a link before routing it.
import 'package:app_links/app_links.dart';
final appLinks = AppLinks();
void listenForLinks(GoRouter router) {
appLinks.uriLinkStream.listen((uri) {
if (uri.host != 'example.com') return;
final target = uri.hasQuery ? '${uri.path}?${uri.query}' : uri.path;
router.go(target);
});
}Validating and securing incoming links
Treat every link as untrusted input. Check the host, allow only known path patterns, and validate IDs before you use them.
> Do not pass a link's query value to a network call or a file path without checking it first.
Write that rule as a callout:
URLs and deep links on Flutter web
On web, the browser address bar is the link. A MaterialApp.router app reads the URL path on its own, so your go_router routes work without a platform handler. You do not need an intent filter or an association file for web.
Deferred deep links and install attribution
A deferred deep link opens a link for a user who does not have the app yet. The app is installed first, then the link is passed to it on first launch. Neither app_links nor go_router does this alone. You need a third-party attribution service or a platform install-referrer mechanism.
Exercise
Exercise
A user who is signed out opens https://example.com/products/42?ref=email. Which path does the login redirect store in the from parameter, and why must it be encoded?
Show answer
It stores /products/42?ref=email, encoded with Uri.encodeComponent. Encoding keeps the ? and = characters from being read as part of the login URL's own query string, so the original link survives the round trip.