Skip to content

Dart Cheat Sheet

Last verified May 2026 — runs in your browser
Dart Cheatsheet
var name = 'Alice';

Variable with inferred type

Basics
String name = 'Alice';

Variable with explicit type

Basics
final age = 30;

Runtime constant (single assignment)

Basics
const pi = 3.14;

Compile-time constant

Basics
late String description;

Lazy-initialized non-nullable

Basics
int double num String bool

Built-in types

Basics
dynamic x = 'anything';

Dynamic type (any)

Basics
var name = 'World';
print('Hello, $name!');

String interpolation

Basics
print('Sum: ${a + b}');

Interpolate expression

Basics
if (x > 0) { ... } else if (x == 0) { ... } else { ... }

If/else

Basics
for (var i = 0; i < 10; i++) { ... }

Classic for loop

Basics
for (var item in list) { ... }

For-in loop

Basics
list.forEach((item) => print(item));

forEach iteration

Basics
while (condition) { ... }

While loop

Basics
switch (x) {
  case 1: ...; break;
  default: ...;
}

Switch statement

Basics
String? maybeName;

Nullable type

Null Safety
name ?? 'Anonymous'

Null-coalescing operator

Null Safety
name ??= 'default';

Assign if null

Null Safety
user?.name?.length

Null-aware chain

Null Safety
name!

Null assertion (bang)

Null Safety
if (name != null) { print(name.length); }

Null promotion after check

Null Safety
var list = [1, 2, 3];

List literal

Collections
List<int> list = [1, 2, 3];

Typed list

Collections
list.add(4);

Append element

Collections
list.removeAt(0);

Remove by index

Collections
list.where((x) => x > 2).toList()

Filter list

Collections
list.map((x) => x * 2).toList()

Map each element

Collections
list.reduce((a, b) => a + b)

Reduce to single value

Collections
var set = {1, 2, 3};

Set literal

Collections
var map = {'a': 1, 'b': 2};

Map literal

Collections
map['key'] = value;

Set map value

Collections
map.containsKey('key')

Check key exists

Collections
[...list1, ...list2]

Spread operator

Collections
[if (c) 'a', for (var i in list) i]

Collection if/for

Collections
int add(int a, int b) => a + b;

Arrow function

Functions
void greet(String name) {
  print('Hello $name');
}

Named function

Functions
void greet({String name = 'World'}) {}

Named parameters with defaults

Functions
void greet({required String name}) {}

Required named parameter

Functions
void log(String msg, [String? tag]) {}

Optional positional parameter

Functions
var f = (int x) => x * 2;

Anonymous function (lambda)

Functions
class User {
  String name;
  int age;
  User(this.name, this.age);
}

Class with constructor

Classes
User({required this.name, this.age = 0});

Named constructor parameters

Classes
User.guest() : name = 'Guest', age = 0;

Named constructor

Classes
class Admin extends User { ... }

Inheritance

Classes
class Duck with Swimmer, Flyer { ... }

Mixins

Classes
abstract class Shape { double area(); }

Abstract class

Classes
class Point {
  final double x, y;
  const Point(this.x, this.y);
}

Immutable class with const constructor

Classes
String get fullName => '$first $last';

Getter

Classes
set age(int v) { _age = v; }

Setter

Classes
static const version = '1.0';

Static member

Classes
Future<String> fetch() async {
  return 'data';
}

Async function

Async
final data = await fetch();

Await a Future

Async
try {
  await risky();
} catch (e) { ... }

Async error handling

Async
Stream<int> counter() async* {
  for (var i = 0; ; i++) yield i;
}

Async generator (Stream)

Async
await for (var event in stream) { ... }

Listen to stream

Async
Future.delayed(Duration(seconds: 1), () => 'done')

Delayed Future

Async
Future.wait([f1, f2, f3])

Wait for multiple futures

Async
Showing 57 of 57 snippets

Dart Cheat Sheet — Null Safety, Async/Await, Classes & Mixins Reference

Dart launched in 2011, picked up sound null safety in Dart 2.12, and is now best known as the language behind Flutter. The cheatsheet below covers 55+ snippets across basics, null safety, collections, functions, classes, and async — the slice reached for daily on client and server. Most trouble in Dart code does not come from forgetting syntax. It comes from quirks that look reasonable until they bite. The `late` modifier defers initialisation but throws a `LateInitializationError` the first time the field is read before it is set, and the failure surfaces far from the missing assignment. The bang `!` says "trust me, not null" and crashes loudly when the value is in fact null, often less helpful than the original null check would have been. `const` and `final` look interchangeable, but `const` means compile-time constant — usable in `const` constructors and canonicalised by the runtime — while `final` is single-assignment at runtime. These are the snippets pulled up while debugging exactly that: the right null-aware operator, the right constructor form, the right async pattern, without rereading the language tour from page one each time.

Common pitfalls in Dart

A few patterns earn their place near the top of any Dart file. `String?` and `String` are distinct types under sound null safety, so a `String?` value cannot be assigned where a `String` is expected without an explicit unwrap (`!`, `??`, or a null check that promotes). The `??=` operator assigns only if the variable is currently null — the idiomatic way to lazily initialise a nullable field without an `if` block. List literals like `[1, 2, 3]` are growable by default, while `List.filled(n, value)` returns a fixed-length list unless `growable: true` is passed, and calling `add` on the fixed one throws at runtime rather than compile time. The cascade `..` returns the receiver instead of the method result, so `final controller = TextEditingController()..addListener(...)` keeps the controller in scope rather than the listener registration. Async functions implicitly wrap their return in a `Future`, so a synchronous-looking `return value` inside `async` still produces a `Future<T>` and must be awaited. The cheatsheet groups all of this into Basics, Null Safety, Collections, Functions, Classes, and Async so the right section is one click away.

  • 55+ practical Dart snippets
  • 6 categories covering core language
  • Null safety operators and patterns
  • Async/await and Streams
  • Classes, mixins, and constructors
  • One-click copy to clipboard

Free. No signup. Your inputs stay in your browser. Ads via Google AdSense (consent required).

By ·