Dart Intro
Dart is a general-purpose, object-oriented programming language created at Google. It was first announced in 2011, reached a stable 2.0 in 2018 with a sound type system, and in 2023 Dart 3 made sound null safety mandatory and added records, patterns and class modifiers. Today Dart is best known as the language behind Flutter, but it is a complete language on its own: you can write command-line tools, servers, scripts and web apps with it.
This course teaches Dart itself — no Flutter required. Every example with a ▶ Run button is compiled and executed for real.
What makes Dart different?
| Feature | What it means for you |
|---|---|
| Sound null safety | A String can never be null. Only String? can. The compiler proves it. |
| Static types + inference | Types are checked at compile time, but you rarely write them: var count = 3; is an int. |
| JIT and AOT compilation | Fast edit-run cycles during development (hot reload), small fast native binaries in production. |
| Compiles to JavaScript and WebAssembly | The same code can run in a browser. |
| Everything is an object | Even 3, true and null are objects with methods. |
| Async built in | Future, Stream, async, await and isolates are part of the core language and libraries. |
| Batteries included | Formatter, analyzer, test runner and package manager ship with the SDK. |
Your first Dart program
Every Dart program starts running in a top-level function called main. The print function writes a line to standard output.
void main() {
print('Hello, Dart!');
}Hello, Dart!
voidmeansmainreturns nothing.- Strings can use single or double quotes; Dart style prefers single quotes.
- Every statement ends with a semicolon.
A taste of modern Dart
The snippet below shows several features you will meet in this course: type inference, a list literal, string interpolation with $, an arrow function and a for-in loop.
void main() {
var languages = ['Dart', 'Kotlin', 'Swift'];
var total = 0;
for (var lang in languages) {
total += lang.length;
print('$lang has ${lang.length} letters');
}
String shout(String s) => s.toUpperCase();
print(shout('total: $total'));
}Dart has 4 letters
Kotlin has 6 letters
Swift has 5 letters
TOTAL: 15
Notice that we never wrote List<String> or int — Dart inferred those types from the values. The variables are still statically typed: assigning a string to total later would be a compile error.
The compiler watches your back
Because Dart is statically typed and null safe, many bugs are caught before the program even starts. Here we try to store null in a non-nullable variable:
void main() {
int score = 10;
score = null;
print(score);
}example.dart:3:11: Error: The value 'null' can't be assigned to a variable of type 'int' because 'int' is not nullable.
score = null;
^
Error: AOT compilation failed
Bad state: Generating AOT kernel dill failed!
The program never runs — the compiler reports the file, line and column of the problem. You will learn how to work with nullable types in the Null Safety lesson.
Where is Dart used?
- Mobile, desktop and web apps with Flutter (Android, iOS, Windows, macOS, Linux, web).
- Command-line tools — the Dart SDK's own tools are written in Dart.
- Servers and cloud functions with packages such as
shelfordart_frog. - Scripts you would otherwise write in Python or Bash, compiled to a single executable with
dart compile exe.
How this course is organised
- Dart Tutorial — syntax, variables, types, operators, control flow, collections and functions.
- Dart OOP — classes, constructors, inheritance, interfaces, mixins, extensions, enums, generics and class modifiers.
- Dart 3 Features — records, patterns and exhaustive
switchexpressions. - Async Dart — futures,
async/await, streams and isolates. - Dart Ecosystem — libraries, packages, JSON and testing.
- Practice — exercises and a complete mini project.
Tip: Change the examples and run them again. Breaking code on purpose and reading the compiler's message is one of the fastest ways to learn a language.
Test yourself
Which function does every Dart program start from?
- start()
- run()
- main()
- init()
What does sound null safety guarantee?
- Variables are never null at all
- A non-nullable type such as
intcan never hold null - Null checks happen only at runtime
- Every variable must be initialised to zero
What is the type of total in var total = 0;?
- dynamic
- num
- int
- Object