Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 727
Harsh Gupta Avatar asked Nov 15 '19 19:11

Harsh Gupta


People also ask

What is the role of runApp () in Flutter?

The runApp() function takes the given Widget and makes it the root of the widget tree. In this example, the widget tree consists of two widgets, the Center widget and its child, the Text widget. The framework forces the root widget to cover the screen, which means the text “Hello, world” ends up centered on screen.

What is the role of runApp ()?

2->runApp()--=>this is the main function that runs the main screen. With help of this widget, you can run those screens that you want to run. This screen is known as the root screen of your app.

What is runApp?

Runapp is one of many adware-type apps that form part of the Pirrit adware family. Typically, apps of this type feed users with unwanted ads, however, they might also gather user details. Research shows that Runapp installs another identical app called MacPerformance (also part of Pirrit adware).

What is the main method in Dart?

The main() function is a predefined method in Dart. This method acts as the entry point to the application. A Dart script needs the main() method for execution. print() is a predefined function that prints the specified string or value to the standard output i.e. the terminal.


2 Answers

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
like image 74
Joe Muller Avatar answered Sep 21 '22 11:09

Joe Muller


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.

like image 45
dubace Avatar answered Sep 21 '22 11:09

dubace