Is there any reason to use one over the other in any given circumstance? I'm trying to figure out why there are two ways of doing this. I'm referring to the "navigate with arguments" cookbook recipe:
https://flutter.dev/docs/cookbook/navigation/navigate-with-arguments
As a result, on opening the app, the route takes the user to a certain page. Good news is, Flutter has a built-in feature for achieving such a feat. Moreover, we can control the navigation at the very beginning of our Flutter app. To do this, in flutter MaterialApp widget, we use onGenerateRoute property.
A route that blocks interaction with previous routes. ModalRoutes cover the entire Navigator. They are not necessarily opaque, however; for example, a pop-up menu uses a ModalRoute but only shows the menu in a small box overlapping the previous route. The T type argument is the return value of the route.
RouteSettings class Null safetyData that might be useful in constructing a Route.
ModalRoute.of
is used to build the route after it has been pushed in the navigation history.
onGenerateRoute
does the same, but before that route is pushed in the navigation history.
ModalRoute.of
is enough for most use-cases. But onGenerateRoute
is more flexible. It allows building the route conditionally based on what the argument is, or type checking that the argument is valid:
onGenerateRoute: (RouteSettings settings) {
if (settings.name == '/custom-route') {
assert(settings.arguments is MyCustomArgument);
}
}
or:
onGenerateRoute: (RouteSettings settings) {
if (settings.name == '/users') {
if (settings.arguments != null) {
return UserDetailsRoute(id: settings.arguments);
}
else {
return UserListRoute();
}
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With