var name = 'Alice';
Variable with inferred type
String name = 'Alice';
Variable with explicit type
final age = 30;
Runtime constant (single assignment)
const pi = 3.14;
Compile-time constant
late String description;
Lazy-initialized non-nullable
int double num String bool
Built-in types
dynamic x = 'anything';
Dynamic type (any)
var name = 'World';
print('Hello, $name!'); String interpolation
print('Sum: ${a + b}'); Interpolate expression
if (x > 0) { ... } else if (x == 0) { ... } else { ... } If/else
for (var i = 0; i < 10; i++) { ... } Classic for loop
for (var item in list) { ... } For-in loop
list.forEach((item) => print(item));
forEach iteration
while (condition) { ... } While loop
switch (x) {
case 1: ...; break;
default: ...;
} Switch statement
String? maybeName;
Nullable type
name ?? 'Anonymous'
Null-coalescing operator
name ??= 'default';
Assign if null
user?.name?.length
Null-aware chain
name!
Null assertion (bang)
if (name != null) { print(name.length); } Null promotion after check
var list = [1, 2, 3];
List literal
List<int> list = [1, 2, 3];
Typed list
list.add(4);
Append element
list.removeAt(0);
Remove by index
list.where((x) => x > 2).toList()
Filter list
list.map((x) => x * 2).toList()
Map each element
list.reduce((a, b) => a + b)
Reduce to single value
var set = {1, 2, 3}; Set literal
var map = {'a': 1, 'b': 2}; Map literal
map['key'] = value;
Set map value
map.containsKey('key') Check key exists
[...list1, ...list2]
Spread operator
[if (c) 'a', for (var i in list) i]
Collection if/for
int add(int a, int b) => a + b;
Arrow function
void greet(String name) {
print('Hello $name');
} Named function
void greet({String name = 'World'}) {} Named parameters with defaults
void greet({required String name}) {} Required named parameter
void log(String msg, [String? tag]) {} Optional positional parameter
var f = (int x) => x * 2;
Anonymous function (lambda)
class User {
String name;
int age;
User(this.name, this.age);
} Class with constructor
User({required this.name, this.age = 0}); Named constructor parameters
User.guest() : name = 'Guest', age = 0;
Named constructor
class Admin extends User { ... } Inheritance
class Duck with Swimmer, Flyer { ... } Mixins
abstract class Shape { double area(); } Abstract class
class Point {
final double x, y;
const Point(this.x, this.y);
} Immutable class with const constructor
String get fullName => '$first $last';
Getter
set age(int v) { _age = v; } Setter
static const version = '1.0';
Static member
Future<String> fetch() async {
return 'data';
} Async function
final data = await fetch();
Await a Future
try {
await risky();
} catch (e) { ... } Async error handling
Stream<int> counter() async* {
for (var i = 0; ; i++) yield i;
} Async generator (Stream)
await for (var event in stream) { ... } Listen to stream
Future.delayed(Duration(seconds: 1), () => 'done')
Delayed Future
Future.wait([f1, f2, f3])
Wait for multiple futures
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 Marco B. ·