Dart Syntax Basics
Prerequisites: How Dart code runs
A few rules cover most of what Dart code looks like.
Statements end with a semicolon.
Curly braces mark blocks of code, and conditions go in parentheses:
void main() {
if (10 > 5) {
print('Ten is bigger');
}
}
Ten is bigger
Indentation is two spaces. Dart doesn't require it, but dart format applies it automatically, so every Dart project looks the same.
Case matters, and naming follows conventions:
lowerCamelCasefor variables, functions, and constants (firstName,maxUsers).UpperCamelCasefor classes, enums, and types (ShoppingCart).lowercase_with_underscoresfor file and package names (shopping_cart.dart).
A leading underscore means private. A name like _cache is only
visible inside its own file (library). There are no public/private
keywords.
Trailing commas help formatting. In Flutter especially, adding a
comma after the last argument tells dart format to put each argument
on its own line.
Common mistakes
- A missing closing brace or parenthesis. The editor highlights matching pairs to help.
- Leaving out the semicolon after a statement.