Why is there the initialRoute property when home already exists in Flutter?
Why is there the initialRoute property when home already exists in Flutter?
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
},
),
);
Home-route is the default route of your app while on other hand the initial route is the first route that is rendered on the screen. both look like same but works in different way. home: the default route of your app, which is rendered on screen if no initial route is given. initial: no matter whether the home route is given or not if the initial route is given then in that case the initial route will be rendered on screen when app launched, there are two behaviors or initial route,
1.InitialRoute will be rendered first on screen, now if the home route is also given then in that case when we press the back button we will move back to home route
If we talk about the home route, there are two case scenarios as well,
let see the code below to understand,
class Home extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: const Text("Home Route"),
),
),
initialRoute: '/init',
routes: <String, WidgetBuilder>{
'/init': (BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text("Initial Route"),
),
);
}
},
);
}
}
since we have given the initial route as well home route so when we launch the app our app will first render the initial route as shown below,
as shown above, the app shows the initial route we defined in the MaterialApp construct, now let's see what will happen when we press the back button

now when we press the back button it takes us back to our home route
now the thing to understand is that if we provided a different home and initial route then the initial route will be rendered and shown on screen first and if the user presses the back button then, in that case, the home route will be rendered on screen.
where to use different initial and home routes?: there are many use cases when we can use different home and initial route cases, a university can show an initial view to the student using which student can apply for admission for a specific time instead of showing home page, a restaurant can show discount offer and register user by showing initial route in case of user press back button they will move back to home route as shown above.