Dart main()

main() function is the entry point to the Dart application.

main() function returns nothing, and can take an optional list of strings for arguments.

Examples

Simple main() Function

In the following example, we write a Dart application, main.dart, with main() function.

main.dart

void main() {
  print('Hello World');
}

Output

Hello World

Now, let us write a main() function that takes command line arguments for its input parameter.

main() Function with Arguments

main.dart

void main(List<String> args) {
  print(args);
}

Output

(base) tutorialkart@DartTutorial % dart main.dart Apple Banana
[Apple, Banana]
ADVERTISEMENT

Conclusion

In this Dart Tutorial, we learned what a main() function is, and how to pass arguments to it via console, with examples.