Warning: Undefined array key "lang_code" in /home/pmfnldmy/public_html/androidatc/ap/extension/classes/common.php on line 800
Dart main( ) function

main( ) function

A function is any close identity which includes a certain code or a collection of statements grouped together to perform an operation. Every app must have a top-level main() function, which serves as the entry point to the app.

The following image displays the main() function structure:

All your Dart code must be written inside the main() function body.

Example 1:

1- Open IntelliJ IDEA
2- Write the following code inside the main function body :

main() {

  print('Welcome to Android ATC');

  print(1+1);

  }


3- Run this Dart file (Click Run menu , then select Run).
The result follows:

Example 2:
Continue using the previous example, and add another function called test()outside the main() function body as illustrated in the code below:

main() {

  print('Welcome to Android ATC');

  print(1+1);

  }

  test(){

  print('Hello Flutter Developers');

  }


When you run this Dart code, you will get the same previous result without any effect to the content of the test() function.

This means that, when you run any Dart code, only the code inside the main()function body will run. Also, if you want to run any function outside the main()function body, you must add a reference to this external function inside the main()function. For more details about Dart functions, check lesson 2 of the Flutter application development course.

* This topic is a part of lesson 2 of Flutter Application Development course. For more information about this course, click here.