Sub topic 15
Deep links and web URLs
Assume you know Dart null safety, Flutter widgets, Navigator, and the basic go_router setup. A deep link is a URL that opens a specific screen rather than only opening the app’s home screen. For example, /books/42?tab=reviews identifies book 42 and asks the screen to show its reviews tab.
With go_router, the URL is the navigation state. When the app starts, the router reads the current location, matches it against the declared routes, and builds the corresponding pages.
Mapping URLs to routes
Declare dynamic parts with a colon. The value after the colon becomes a path parameter. Query parameters follow a ? and are read separately.
import 'package:flutter/material.dart';
import 'package:go_router/go_router.dart';
final GoRouter router = GoRouter(
routes: [
GoRoute(
path: '/',
builder: (context, state) => const HomeScreen(),
),
GoRoute(
path: '/books/:id',
builder: (context, state) {
final String id = state.pathParameters['id']!;
final String tab =
state.uri.queryParameters['tab'] ?? 'summary';
return BookScreen(id: id, tab: tab);
},
),
],
);
void main() {
runApp(
MaterialApp.router(
routerConfig: router,
),
);
}
class HomeScreen extends StatelessWidget {
const HomeScreen({super.key});
@override
Widget build(BuildContext context) {
return Scaffold(
body: Center(
child: ElevatedButton(
onPressed: () => context.go('/books/42?tab=reviews'),
child: const Text('Open reviews'),
),
),
);
}
}
class BookScreen extends StatelessWidget {
const BookScreen({
super.key,
required this.id,
required this.tab,
});
final String id;
final String tab;
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: Text('Book $id')),
body: Center(child: Text('Selected tab: $tab')),
);
}
}state.pathParameters contains values that identify a resource. Query parameters usually control a view, filter, sort order, or optional setting. Encode values when constructing URLs if they may contain spaces or reserved characters. Prefer route names and generated helpers when possible, because they reduce mistakes in URL spelling.
Web hosting and mobile links
On the web, browser navigation and refresh must reach Flutter’s entry point. Configure the web server to return index.html for application routes. Without this fallback, opening /books/42 directly can produce a server 404 even though the route exists in Flutter.
A web URL also appears in browser history. context.go changes the application location and rebuilds the matching route stack. The browser’s Back button can then return to the previous location.
On Android, configure an intent filter for the domain or URL pattern. On iOS, configure universal links and the associated domain. Those platform settings connect an external HTTPS link to the installed app. go_router handles the route after the operating system delivers the link.
Exercise
What should the route /books/7?tab=details extract from the location?
Show answer
It should extract id as the path parameter 7 and tab as the query parameter details.