What is the difference between main function and the runApp() function in Flutter?
Asked Answered
P

4

9

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?

Poach answered 15/11, 2019 at 19:46 Comment(0)
W
10

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
Wringer answered 10/6, 2020 at 4:34 Comment(0)
F
6

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.

Fbi answered 15/11, 2019 at 20:17 Comment(0)
C
3

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.

Collage answered 15/11, 2019 at 22:27 Comment(0)
H
3

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.

Hemistich answered 10/6, 2020 at 5:4 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.