dart2wasm
- Dart course
- Advanced
- Lesson 50/83
- 5 min
Learn WebAssembly with dart2wasm.
dart2wasm
dart2wasm is the tool that compiles Dart code to WebAssembly (WASM). It arrived with Dart 3; it runs faster than dart2js. You compile with dart compile wasm main.dart -o main.wasm; the output is a .wasm file.
WebAssembly is a low-level format the browser runs at near-native speed. dart2wasm brings Dart's garbage collector (GC) and type system to WASM. It does AOT (ahead-of-time) compilation so there is no interpreter overhead; it is significantly faster than dart2js.
WebAssembly advantages
WebAssembly (WASM) is a portable binary format the browser runs fast. dart2wasm compiles Dart code into this format. Advantages: type-safe, garbage-collected (GC), AOT-compiled and faster than dart2js.
However WASM is still evolving; in some browsers dart2js may work more compatibly. But the Dart team positions dart2wasm as the future standard. New web projects prefer dart2wasm; if old browser support is needed dart2js stays as a fallback.
Practice Print dart2wasm info
In main add print('dart2wasm: Dart → WebAssembly') and print('Output: main.wasm').
Starter code
void main() {
// print dart2wasm info
}Sample solution
void main() {
print('dart2wasm: Dart → WebAssembly');
print('Output: main.wasm');
}Expected output
dart2wasm: Dart → WebAssembly Output: main.wasm
Questions and answers
Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.
1 The dart2wasm output extension
dart compile wasm main.dart -o main.____;
dart compile wasm main.dart -o main.wasm;
Explanation: dart2wasm produces a .wasm WebAssembly file.
2 With which Dart version did dart2wasm arrive?
- Dart 2
- Dart 1
- Dart 3 Correct answer
- Dart 0.5
Explanation: dart2wasm arrived with Dart 3.
3 Match the dart2wasm parts
| Term | Meaning |
|---|---|
dart2wasm |
Dart → WebAssembly |
.wasm |
Output file |
AOT |
Ahead-of-time compile |
Dart 3 |
Version |