Why use onGenerateRoute() vs ModalRoute.of() in Flutter?
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:
1 2 3 4 5 6 7 8 9 10 |
onGenerateRoute: (RouteSettings settings) { if (settings.name == '/users') { if (settings.arguments != null) { return UserDetailsRoute(id: settings.arguments); } else { return UserListRoute(); } } } |
If you like this question & answer and want to contribute, then write your question & answer and email to freewebmentor[@]gmail.com. Your question and answer will appear on FreeWebMentor.com and help other developers.