Dart design
- Dart course
- Expert
- Lesson 1/47
- 5 min
Learn Dart language design principles.
Dart design
Dart is a language that targets fast compilation, runtime efficiency and developer productivity together. Sound null safety, a static type system and a dual JIT/AOT mode are the result of these goals.
The same source can compile to both web and native platforms. Static checking is the default; dynamic is the escape hatch. These lessons cover the language design decisions and the reasoning behind them.
Static and dynamic balance
Dart's type system is static by default; most errors are caught at compile time. Still, the dynamic type is available when runtime checks are needed.
Object is the supertype of all objects but method calls are checked at compile time. This balance combines safety with flexibility: the safe path is open, and the escape hatch is available. Dart's philosophy is 'safe defaults, flexibility when needed'.
Practice A Point class with static types
Define a Point class with final int x; and final int y; fields, a Point(this.x, this.y); constructor and an int sum() => x + y; method. In main create final p = Point(3, 4);; print the result with print(p.sum());.
Starter code
void main() {
// define a Point class (x, y fields; sum method)
// create a Point; print the sum() result
}Sample solution
class Point {
final int x;
final int y;
Point(this.x, this.y);
int sum() => x + y;
}
void main() {
final p = Point(3, 4);
print(p.sum());
}Expected output
7
Questions and answers
Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.
1 What is one of Dart's design goals?
- No garbage collector
- Sound null safety Correct answer
- Manual memory management
- Browser only
Explanation: sound null safety is one of Dart's core design goals.
2 Assign a value to a String variable
String greeting = ____;
print(greeting);
String greeting = 'Hello';
print(greeting);
Explanation: 'Hello' is a String value and passes static checking.
3 Match the Dart design parts
| Term | Meaning |
|---|---|
Sound null safety |
Null reference safety |
Static type system |
Compile-time checking |
AOT compilation |
Single binary (native) |
JIT compilation |
Fast development |