dinesh balan
dinesh balan

Reputation: 348

Flutter Modular redirect to 404 page

I used flutter_modular for flutter web to navigation.

import 'package:flutter_modular/flutter_modular.dart';

 class AppModule extends Module {
   @override
   final List<Bind> binds = [];

   @override
   final List<ModularRoute> routes = [
       ChildRoute('/', child: (_, __) => HomeScreen()),
       ChildRoute('/signin', child: (_, __) => SignInPage()),
       ChildRoute('/login', child: (_, __) => LogInPage()),
       ChildRoute('/404', child: (_, __) => Custome404()),
  ];
 }

if unfortunately user typed URL like example.com/gfhgfhb/. The flutter modular plugin show error Error: RouteNotFoundException: Route (/rferwg) not found. Now who can I set default 404 page in code?

Upvotes: 3

Views: 1355

Answers (2)

Hooman
Hooman

Reputation: 793

you can use WildcardRoute :

WildcardRoute(child: (context, args) => NotFoundPage()),

according to its documentation :

Have only one WildcardRoute per module and, if possible, let it be the last element.

Upvotes: 1

Kantine
Kantine

Reputation: 781

Do you use a MaterialApp, CupertinoApp, or WidgetsApp somewhere ? If so, you could use the onUnknownRoute property:

onUnknownRoute: (settings) {
  return MaterialPageRoute(builder: (_) => PageNotFound());
},

https://medium.com/flutter/handling-404-page-not-found-error-in-flutter-731f5a9fba29

Upvotes: 0

Related Questions