Dart Syntax
Dart's syntax belongs to the C family: curly braces mark blocks, semicolons end statements, and code is case-sensitive. If you know Java, C#, JavaScript or Kotlin, most of it will look familiar.
Anatomy of a program
// A top-level constant
const appName = 'TinyShop';
// A top-level function with a typed parameter and return type
int square(int n) {
return n * n;
}
void main() {
var side = 7; // a local variable
print('$appName: ${square(side)}');
}TinyShop: 49
| Piece | Rule |
|---|---|
| Statements | End with ; |
| Blocks | Wrapped in { } |
| Top level | Functions, variables, classes, enums and more can live directly in a file — no wrapper class needed |
| Entry point | void main() or void main(List<String> args) |
| Case | count, Count and COUNT are three different names |
Comments
// Single-line comment
/* Multi-line
comment */
/// Documentation comment — used by `dart doc` and shown by IDEs.
/// Refer to identifiers with square brackets: [square].
int square(int n) => n * n;Use /// above public classes, functions and fields. The analyzer can even warn you when public APIs lack docs.
Identifiers and naming conventions
Identifiers may contain letters, digits, _ and $, but cannot start with a digit. Dart's official style guide is followed almost universally:
| Kind | Style | Example |
|---|---|---|
| Classes, enums, typedefs, extensions | UpperCamelCase |
ShoppingCart |
| Variables, functions, parameters, constants | lowerCamelCase |
itemCount, maxRetries |
| Libraries, packages, files | lowercase_with_underscores |
shopping_cart.dart |
Note: Unlike many languages, Dart constants use
lowerCamelCasetoo:const defaultTimeout = 30;, notDEFAULT_TIMEOUT.
Privacy with an underscore
Dart has no public/private keywords. A name starting with _ is private to its library (its file). Everything else is public.
class Counter {
int _value = 0; // private: only visible in this file
void increment() => _value++;
int get value => _value; // public read-only access
}
void main() {
final c = Counter();
c.increment();
c.increment();
print(c.value);
}2
Expressions vs statements
Almost everything that produces a value is an expression: 2 + 3, name.length, a function call, even cond ? a : b. Statements do something: if, for, return, variable declarations. Dart lets you use expression-bodied functions with =>:
bool isEven(int n) => n % 2 == 0;
String label(int n) => isEven(n) ? 'even' : 'odd';
void main() {
for (var i = 1; i <= 3; i++) {
print('$i is ${label(i)}');
}
}1 is odd
2 is even
3 is odd
=> expr is shorthand for { return expr; } and works only with a single expression.
Printing
print() accepts any object and calls its toString():
void main() {
print(42);
print(3.5);
print(true);
print([1, 2, 3]);
print({'a': 1});
print(null);
print('Multiple ' 'adjacent ' 'strings are joined');
}42
3.5
true
[1, 2, 3]
{a: 1}
null
Multiple adjacent strings are joined
Adjacent string literals are concatenated at compile time — useful for splitting long text across lines.
Common syntax mistakes
A missing semicolon is a compile error, and the compiler tells you exactly where:
void main() {
var x = 5
print(x);
}example.dart:2:11: Error: Expected ';' after this.
var x = 5
^
Error: AOT compilation failed
Bad state: Generating AOT kernel dill failed!
Test yourself
How do you make a class member private in Dart?
- Use the
privatekeyword - Start its name with
_ - Declare it with
final - Put it in a
hiddenblock
Which naming style does Dart use for constants?
- SCREAMING_CAPS
- lowerCamelCase
- UpperCamelCase
- snake_case
What does int twice(int n) => n * 2; mean?
- A function returning
n * 2 - A lambda that is never called
- A getter
- A compile error