generateRoute method
Generates a MaterialPageRoute for a given route based on the provided route settings.
routeSettings
: The settings for the route to be generated.
Returns a MaterialPageRoute for the specified route, including any associated args.
If the route is not found in the routes
map, it will use the initialRoute
as the default route.
If the route is not found and no initialRoute
is provided, it will return an error page.
Implementation
MaterialPageRoute<void> generateRoute(RouteSettings routeSettings) {
final routeBuilder = routes[routeSettings.name];
if (routeBuilder != null) {
Map<String, dynamic>? args = routeSettings.arguments == null
? null
: routeSettings.arguments as Map<String, dynamic>;
return MaterialPageRoute<void>(
settings: routeSettings,
builder: (context) => routeBuilder(context, args),
);
}
// If the route is not found, you can return a default route or an error page.
return MaterialPageRoute<void>(
settings: routeSettings,
builder: (context) => initialRoute,
/// Default route
);
}