Why is there the initialRoute
property when home
already exists in Flutter?
home. The widget for the default route of the app (Navigator. defaultRouteName, which is / ). This is the route that is displayed first when the application is started normally, unless initialRoute is specified. It's also the route that's displayed if the initialRoute can't be displayed.
initialRoute. The name of the first route to show, if a Navigator is built.
home: This property takes in widget as the object to show on the default route of the app. initialRoute: This property takes in a string as the object to give the name of the first route in which the navigator is built. locale: It provides a locale for the MaterialApp.
It's more about code readability (but not limited to), see all of them are doing the same job but in different ways:
runApp(
MaterialApp(
home: HomePage(),
),
);
or
runApp(
MaterialApp(
initialRoute: '/',
routes: {
'/': (_) => HomePage(),
},
),
);
Use of onGenerateInitialRoute
/onGenerateRoute
allows you to change navigator animation too.
runApp(
MaterialApp(
onGenerateInitialRoutes: (route) {
return [
MaterialPageRoute(builder: (_) => HomePage())
];
}
),
);
or
runApp(
MaterialApp(
initialRoute: '/',
onGenerateRoute: (settings) {
if (settings.name == '/') return MaterialPageRoute(builder: (_) => HomePage());
return MaterialPageRoute(builder: (_) => UnknownPage()); // you can do this in `onUnknownRoute` too
},
),
);
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