Sub topic 14
Handling Unknown Locations in Flutter with go_router
This chapter assumes you know Dart null safety, can build Flutter widgets, and understand basic Navigator.push and Navigator.pop calls. It focuses on what happens when a URL does not match any declared GoRoute.
What counts as an unknown location?
A location is the URL-style value that identifies the current route. For example, /books/42 is a location. go_router compares that location with the paths in your route tree.
A location is unknown when no route matches it. Common causes include:
- A user enters a mistyped web URL.
- A deep link points to a route that the app no longer declares.
- A path contains a missing or invalid segment.
- A redirect sends the router to an undeclared location.
Without custom handling, go_router displays its default error screen. A production app should usually provide its own error page with a useful message and a way back to a valid location.
Configure `errorBuilder`
The errorBuilder callback belongs to GoRouter. It receives the current BuildContext and GoRouterState. The state contains the failed location and the routing error.
import 'package:flutter/material.dart';
import 'package:go_router/go_router.dart';
final GoRouter router = GoRouter(
initialLocation: '/',
errorBuilder: (context, state) {
return ErrorScreen(
location: state.uri.toString(),
message: state.error.toString(),
);
},
routes: <RouteBase>[
GoRoute(
path: '/',
builder: (context, state) => const HomeScreen(),
),
GoRoute(
path: '/books/:id',
builder: (context, state) {
return Text('Book ${state.pathParameters['id']}');
},
),
],
);
void main() {
runApp(
MaterialApp.router(
routerConfig: router,
),
);
}
class HomeScreen extends StatelessWidget {
const HomeScreen({super.key});
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: const Text('Home')),
body: Center(
child: ElevatedButton(
onPressed: () => context.go('/missing'),
child: const Text('Try an unknown location'),
),
),
);
}
}
class ErrorScreen extends StatelessWidget {
const ErrorScreen({
super.key,
required this.location,
required this.message,
});
final String location;
final String message;
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: const Text('Page not found')),
body: Center(
child: Padding(
padding: const EdgeInsets.all(24),
child: Column(
mainAxisSize: MainAxisSize.min,
children: [
Text('No route matches $location'),
const SizedBox(height: 8),
Text(message),
const SizedBox(height: 16),
ElevatedButton(
onPressed: () => context.go('/'),
child: const Text('Return home'),
),
],
),
),
),
);
}
}state.uri identifies the requested location. state.error explains the routing failure. Show a friendly message to users, but send detailed error information to your logging system rather than exposing internal details in a public app.
How the fallback is selected
For a web app, test unknown locations by entering them directly in the address bar. Also test refreshes, because a deep link must be understood when the app starts, not only after an in-app button is pressed.
Exercise
What should happen when the user opens /books if the only book route is /books/:id?
Show answer
The location is unknown because it lacks the required id segment, so go_router invokes errorBuilder.