Sub topic 15
URLs and Deep Links on Flutter Web
Assume you know basic Dart, Flutter widgets, URL parts, and go_router. You also need access to the web server that hosts your Flutter build.
Flutter web treats a deep link as a normal browser URL. For example:
https://shop.example.com/products/42?ref=emailThe browser loads the Flutter application, and the router chooses the screen for /products/42. Unlike mobile custom schemes, this URL can also work when the app is not installed because it is an ordinary HTTPS address.
Choose a URL strategy
Flutter web supports two common URL styles:
| Strategy | Example | Main trade-off |
|---|---|---|
| Hash | `/#/products/42` | Usually works without server rewrite rules |
| Path | `/products/42` | Cleaner URLs, but the server must return `index.html` for app routes |
Path URLs are usually better for public pages and shared links. Enable them before starting the application:
import 'package:flutter/material.dart';
import 'package:flutter_web_plugins/url_strategy.dart';
import 'package:go_router/go_router.dart';
void main() {
usePathUrlStrategy();
runApp(const ShopApp());
}
final router = GoRouter(
routes: [
GoRoute(
path: '/',
builder: (context, state) => const HomePage(),
),
GoRoute(
path: '/products/:id',
builder: (context, state) {
final id = state.pathParameters['id']!;
final ref = state.uri.queryParameters['ref'];
return ProductPage(id: id, ref: ref);
},
),
],
errorBuilder: (context, state) => const NotFoundPage(),
);
class ShopApp extends StatelessWidget {
const ShopApp({super.key});
@override
Widget build(BuildContext context) {
return MaterialApp.router(routerConfig: router);
}
}
class HomePage extends StatelessWidget {
const HomePage({super.key});
@override
Widget build(BuildContext context) {
return const Scaffold(body: Center(child: Text('Home')));
}
}
class ProductPage extends StatelessWidget {
const ProductPage({required this.id, this.ref, super.key});
final String id;
final String? ref;
@override
Widget build(BuildContext context) {
return Scaffold(body: Center(child: Text('Product $id, ref: $ref')));
}
}
class NotFoundPage extends StatelessWidget {
const NotFoundPage({super.key});
@override
Widget build(BuildContext context) {
return const Scaffold(body: Center(child: Text('Page not found')));
}
}state.pathParameters reads values embedded in the path. state.uri.queryParameters reads optional query values. The browser address bar and back button stay synchronized with router navigation.
Configure the server fallback
A direct request to /products/42 reaches the server before Flutter runs. The server must serve the Flutter app's index.html instead of returning a 404. Configure this rewrite for every client-side route, while keeping real static files available.
On Flutter web, HTTPS links are web routes first. Mobile App Links or Universal Links may open the installed mobile application, but the web deployment still needs its own route handling and server fallback.
Exercise
Why can a Flutter web app show a 404 when navigation inside the app works?
Show answer
The server received the deep URL directly and did not rewrite it to index.html, so Flutter never started to handle the route.