Dart Language and SDK tool

Version 3.13 · source

Start here

dart create <name>
New project
dart run
Run the app
dart pub add <pkg>
Add a package
dart test
Run tests
dart format .
Format code
dart analyze
Find problems
dart compile exe <file>
Native binary
dart pub upgrade
Update deps

Concepts

  1. pubspec.yaml declares deps
  2. pubspec.lock pinned versions
  3. bin/ + lib/ your code
  4. exe / js / wasm output

Project

dart create <name>
New console app
dart create -t package <name>
New library package
dart create -t web <name>
New web app
dart info
Show tooling diagnostics
dart --version
Print SDK version
dart devtools
Open DevTools

Run and compile

dart run
Run the package's main
dart run <file.dart>
Run one file
dart run <pkg>:<script>
Run a package executable
dart run --enable-asserts <file>
Run with asserts on
dart compile exe <file.dart>
AOT native executable
dart compile kernel <file.dart>
Portable kernel module
dart compile js <file.dart>
Compile to JavaScript
dart compile wasm <file.dart>
Compile to WebAssembly
dart compile aot-snapshot <file>
AOT snapshot
dart compile exe -o <out> <file>
Name the output file

Packages

dart pub add <pkg>
Add a dependency
dart pub add dev:<pkg>
Add a dev dependency
dart pub remove <pkg>
Remove a dependency
dart pub get
Fetch locked deps
dart pub upgrade
Upgrade within constraints
dart pub upgrade --major-versions
Raise constraints too
dart pub outdated
Show newer versions
dart pub deps
Print dependency tree
dart pub downgrade
Resolve lowest versions
dart pub publish --dry-run
Check a package for pub.dev
dart pub global activate <pkg>
Install a CLI tool
dart pub global run <pkg>:<cmd>
Run a global tool

Quality

dart format .
Format all files
dart format --output=none --set-exit-code .
Check formatting (CI)
dart analyze
Analyze the package
dart analyze --fatal-infos
Fail on infos too
dart fix --dry-run
Preview automated fixes
dart fix --apply
Apply automated fixes
dart doc .
Generate API docs
dart test
Run all tests
dart test <path>
Run one test file
dart test -n <name>
Filter tests by name
dart test -p chrome
Run in a browser
dart test --coverage=<dir>
Collect coverage

Language basics

var x = 1; final y = 2; const z = 3;
Mutable, final, compile-time
String? s;
Nullable type
s?.length ?? 0
Null-aware access with default
s!
Assert non-null
late final T x;
Initialize later
'Hi $name, ${a + b}'
String interpolation
int add(int a, int b) => a + b;
Arrow function
void f({required int a, int b = 0})
Named parameters
void f(int a, [int? b])
Optional positional
class P { final int x; P(this.x); }
Class with ctor shorthand
class A extends B with M implements I
Inheritance, mixin, interface
sealed class S {}
Closed hierarchy
enum Color { red, green }
Enum
extension E on String { }
Extension method
switch (v) { case 1 => 'a', _ => 'b' }
Switch expression
var (a, b) = (1, 2);
Record destructuring

Collections and async

[1, 2, 3]
List literal
{'a': 1}
Map literal
{1, 2}
Set literal
[for (var i in xs) i * 2]
Collection for
xs.map((x) => x * 2).toList()
Transform a list
xs.where((x) => x > 1)
Filter
xs.fold(0, (a, b) => a + b)
Reduce
Future<T> f() async { await g(); }
Async function
await Future.wait([a(), b()])
Run futures together
Stream<T> f() async* { yield 1; }
Stream generator
await for (var e in stream) { }
Consume a stream
try { } on E catch (e) { } finally { }
Handle errors
Isolate.run(() => work())
Compute in another isolate

Recipes

New CLI app with a dependency
dart create hellocd hellodart pub add argsdart run
CI quality gate
dart pub getdart format --output=none --set-exit-code .dart analyze --fatal-infosdart test
Ship a native binary
dart compile exe bin/hello.dart -o hello./hello
Keep deps fresh
dart pub outdateddart pub upgrade --major-versionsdart test