I tend to ask this question because most of the time we directly call the runApp function main and do nothing else. My question is that why was runApp and main kept different? It could have been simple that either main function or runApp function was kept and other was discarded?
In Dart, main() acts as the entry point for the program whereas runApp() attaches the given widget to the screen.
According to this post, it's possible to establish configuration variables before actually attaching the first widget. This makes the separation between main() and runApp() pretty useful. For example, you can update all of the following before attaching the first widget:
- Theme colors
- Home page
- User's sign-in status
- Version-specific widgets
main ()
function came from Java-like languages so it's where all program started, without it, you can't write any program on Flutter even without UI.
And runApp()
function should return Widget that would be attached to the screen as a root of the Widget Tree that will be rendered.
main()
is needed for every Dart program—it's the entry point for the app. In Flutter apps, this should also call runApp()
to start the framework.
Flutter is a Framework, whereas Dart is a language.
main() function starts execution of Dart Language, which in turn provides methods to load Flutter Components which is runApp() method, which works as main function for Flutter Framework.
so In short main is the starting point of execution for Dart Language and runApp() is the same for Flutter Framework.
© 2022 - 2024 — McMap. All rights reserved.