The main() function
Every app must have a top-level main()
function, which serves as theentrypoint to the app. The main()
function returns void
and has anoptional List<String>
parameter for arguments.
Here’s an example of the main()
function for a web app:
void main() {
querySelector('#sample_text_id')
..text = 'Click me!'
..onClick.listen(reverseText);
}
Note: The ..
syntax in the preceding code is called a cascade. With cascades, you can perform multiple operations on the members of a single object.
Here’s an example of the main()
function for a command-line app thattakes arguments:
// Run the app like this: dart args.dart 1 test
void main(List<String> arguments) {
print(arguments);
assert(arguments.length == 2);
assert(int.parse(arguments[0]) == 1);
assert(arguments[1] == 'test');
}
You can use the args library todefine and parse command-line arguments.