Dart Syntax
Dart Syntax
In the previous chapter, we created a Dart file called main.dart.
We used this code to print "Hello World" to the screen:
Result:
Hello World
Example Explained
Every Dart program starts in a function called main().
Dart runs the code inside the curly brackets {} of main(), from top to bottom.
The word void means that the function does not return (give back) a value.
print() is a function that writes a line of text to the screen.
The text 'Hello World' is a string.
In Dart, you can write strings with single quotes or double quotes.
Statements and Semicolons
A statement is one instruction for the computer.
In Dart, every statement must end with a semicolon ;.
This program has three statements, and Dart runs them in order:
Example
void main() {
print('Dart is fun');
print('Dart is fast');
print('Dart is used by Flutter');
}
Try it Yourself »
Result:
Dart is fun
Dart is fast
Dart is used by Flutter
If you forget a semicolon, the program does not run.
Dart shows an error with the line and the column of the problem:
Result:
main.dart:2:22: Error: Expected ';' after this.
print('Hello World')
^
Upper and Lower Case
Dart is case-sensitive.
print and Print are two different names.
Only print exists, so this program does not run:
Result:
main.dart:2:3: Error: Method not found: 'Print'.
Print('Hello World');
^^^^^
Note: Extra spaces and new lines do not matter to Dart.
We use them to make the code easier to read.