Sub topic 11
Showing a Fallback for Unknown Links
Assume you know basic Dart and Flutter widgets, understand URI parts, and have used go_router with MaterialApp.router. This chapter focuses on links that are valid enough to reach the app but do not match a known route. It also covers links that should be rejected before routing.
Unknown and malformed links
An unknown link has a valid shape but no matching route. For example, /products/42 may be supported while /products/unknown/action is not.
A malformed link cannot be safely interpreted. It might have an unexpected scheme, host, or missing identifier. A link handler should validate external input before passing it to the router.
Use a fallback screen instead of leaving the user on a blank page or showing an internal exception. The fallback should:
- Explain that the destination is unavailable.
- Show a safe, human-readable description.
- Offer a way back to the home screen.
- Avoid displaying sensitive query parameters or internal errors.
Fallback routing with go_router
go_router calls errorBuilder when no declared route matches the incoming location. The GoRouterState contains the URI that caused the failure, which lets the fallback show useful context without manually parsing the original platform event.
import 'package:flutter/material.dart';
import 'package:go_router/go_router.dart';
final router = GoRouter(
routes: [
GoRoute(
path: '/',
builder: (context, state) => const HomeScreen(),
),
GoRoute(
path: '/products/:id',
builder: (context, state) {
final id = state.pathParameters['id']!;
return ProductScreen(id: id);
},
),
],
errorBuilder: (context, state) {
return UnknownLinkScreen(uri: state.uri);
},
);
class UnknownLinkScreen extends StatelessWidget {
const UnknownLinkScreen({required this.uri, super.key});
final Uri uri;
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: const Text('Page not found')),
body: Padding(
padding: const EdgeInsets.all(24),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
const Text('This link does not point to a page in the app.'),
const SizedBox(height: 16),
Text('Requested path: ${uri.path}'),
const SizedBox(height: 24),
ElevatedButton(
onPressed: () => context.go('/'),
child: const Text('Go home'),
),
],
),
),
);
}
}Keep the displayed value limited to uri.path. Query parameters can contain tokens, email addresses, or other private data.
Validate before routing
When handling links manually, validate the scheme and host before calling router.go. Then check required path segments and identifiers. A missing or unexpected value should lead to a safe fallback rather than a request to a backend service.
Common mistakes
- Registering a broad route such as
/:anythingand accidentally hiding unknown-link errors. - Assuming a product ID is valid because it is numeric.
- Showing the complete URI, including secrets in query parameters.
- Redirecting to home without recording enough diagnostic information for development.
- Treating a failed route match as proof that the network or backend is unavailable.
A useful distinction is that the router handles navigation shape, while the screen or repository handles resource existence and authorization.
Exercise
What should happen when an incoming link uses the correct host but its path does not match any declared route?
Show answer
The router should display a dedicated fallback screen, such as the screen returned by go_router's errorBuilder, instead of silently opening an unrelated page.