Summary - Web Dart
- Dart course
- Advanced
- Lesson 54/83
- 5 min
Combine dart2js, dart:html and js_interop to write a web component.
Summary - Web Dart
In this group we learned the ways to bring Dart to the web. dart2js compiles Dart to JavaScript; dart2wasm compiles to WebAssembly and is faster. dart:html is the browser DOM library; dart:js_interop builds a type-safe bridge with JS; jaspr is a reactive web framework.
In this summary we will write a small program that brings dart2js and dart2wasm together. The program statically prints what each compiler produces. This is a small map of the web Dart ecosystem.
Practice Web Dart summary
Add import 'dart:js_interop';. Declare @ JS() external void consoleLog(JSString msg);. In main add print('Summary: Web Dart'), print('dart2js → JavaScript') and print('dart2wasm → WebAssembly').
Starter code
// import the dart:js_interop package
// add @ JS() external consoleLog declaration
void main() {
// print the summary message
}Sample solution
import 'dart:js_interop';
@JS()
external void consoleLog(JSString msg);
void main() {
print('Summary: Web Dart');
print('dart2js → JavaScript');
print('dart2wasm → WebAssembly');
}Expected output
Summary: Web Dart dart2js → JavaScript dart2wasm → WebAssembly
Questions and answers
Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.
1 Which tool compiles to WebAssembly?
- dart2js
- dart2wasm Correct answer
- dart:html
- jaspr
Explanation: dart2wasm compiles Dart to WebAssembly.
2 Match the web Dart tools
| Term | Meaning |
|---|---|
dart2js |
JavaScript |
dart2wasm |
WebAssembly |
dart:html |
DOM |
dart:js_interop |
JS bridge |
3 The StatelessComponent connector
class App extends ____ { ... }
class App extends StatelessComponent { ... }
Explanation: a jaspr component extends StatelessComponent.