認識されないURLを開こうとする等エラーを出した場合に、エラーページのスクリーンウィジェットに飛ばす処理を行う。
errorBuilder:
GoRouterの中でerrorBuilderを指定してエラー処理を行うスクリーンに画面遷移させる
GoRouter(
/* ... */
errorBuilder: (context, state) => ErrorScreen(state.error),
);
【実装】
- errorBuilder: (context, state) => const NotFoundScreen(),
↓のページに飛ばす
- class NotFoundScreen extends StatelessWidget {
- const NotFoundScreen({super.key});
- @override
- Widget build(BuildContext context) {
- return Scaffold(
- appBar: AppBar(),
- body: Center(
- child: Column(
- children: [
- const Text('404 - Page not found'),
- TextButton(
- onPressed: () => context.goNamed(AppRoute.home.name),
- child: const Text("Go Home")),
- ],
- ),
- ),
- );
- }
- }