Dart Cheat Sheet
Last updated
Hello World & program structure
Every Dart program starts at the top-level main function.
| Operation | Syntax |
|---|---|
| Entry point | void main() { ... } |
| Print a line | print("Hello, World!"); |
| String interpolation | print("Hi $name"); |
| Expression in interpolation | print("Sum: ${a + b}"); |
| Comment | // this is a comment |
| Multi-line comment | /* ... */ |
| Import a library | import 'dart:math'; |
| Run a file | dart run main.dart |
Variables & types
Dart is statically typed but can infer types with var.
| Operation | Syntax |
|---|---|
| Inferred variable | var age = 30; |
| Explicit type | int age = 30; |
| Compile-time constant | const pi = 3.14; |
| Runtime constant | final name = getName(); |
| Basic types | int, double, String, bool |
| Dynamic type | dynamic x = 5; |
| Type conversion | int.parse("42"), n.toString() |
| Check type | x is String, x as String |
Null safety
Types are non-nullable by default; add ? to allow null.
| Operation | Syntax |
|---|---|
| Non-nullable (default) | int count = 0; |
| Nullable type | String? name; |
| Null-aware access | user?.name |
| Null-coalescing | name ?? "default" |
| Null-coalescing assign | name ??= "default"; |
| Assert non-null | name! |
| Late initialization | late String value; |
| Null-aware spread | [...?maybeList] |
Strings
Strings support single, double, and triple quotes.
| Operation | Syntax |
|---|---|
| Length | s.length |
| Uppercase / lowercase | s.toUpperCase(), s.toLowerCase() |
| Interpolation | "Total: $price" |
| Concatenate | "foo" + "bar" |
| Contains | s.contains("ell") |
| Starts / ends with | s.startsWith("he") |
| Split | "a,b,c".split(",") |
| Replace | s.replaceAll("a", "b") |
| Substring | s.substring(0, 3) |
| Trim | s.trim() |
Collections (List, Map, Set)
Three core collection types with literal syntax.
| Operation | Syntax |
|---|---|
| List literal | var nums = [1, 2, 3]; |
| Add to list | nums.add(4); |
| Access / length | nums[0], nums.length |
| Map / where | nums.map((n) => n * 2), nums.where((n) => n > 1) |
| Map literal | var ages = {"Ada": 30}; |
| Map access | ages["Ada"] |
| Set literal | var ids = {1, 2, 3}; |
| Spread operator | var all = [...a, ...b]; |
| Collection if / for | [if (show) 1, for (n in xs) n] |
Control flow
Conditions go in parentheses; switch supports patterns.
| Operation | Syntax |
|---|---|
| If / else | if (x > 0) { ... } else { ... } |
| Ternary | var r = x > 0 ? "pos" : "neg"; |
| Switch | switch (n) { case 1: ...; default: ... } |
| For loop | for (var i = 0; i < 10; i++) { ... } |
| For-in loop | for (var item in items) { ... } |
| forEach | items.forEach((x) => print(x)); |
| While loop | while (x < 100) { ... } |
| Do-while | do { ... } while (x < 100); |
| Break / continue | break;, continue; |
Functions
Functions are first-class; arrow syntax shortens single expressions.
| Operation | Syntax |
|---|---|
| Define a function | int add(int a, int b) { return a + b; } |
| Arrow function | int square(int x) => x * x; |
| Optional positional | void log(String m, [int? code]) { ... } |
| Named parameters | void box({int w = 0, int h = 0}) { ... } |
| Required named | void box({required int w}) { ... } |
| Anonymous function | var f = (x) => x * 2; |
| Pass as argument | nums.map((n) => n * 2) |
| Typedef | typedef IntOp = int Function(int); |
Classes & constructors
Classes hold state and behavior; constructors come in several forms.
| Operation | Syntax |
|---|---|
| Define a class | class Point { int x; int y; } |
| Constructor | Point(this.x, this.y); |
| Named constructor | Point.origin() : x = 0, y = 0; |
| Create instance | var p = Point(1, 2); |
| Method | double dist() { ... } |
| Getter | int get area => w * h; |
| Inheritance | class Circle extends Shape { ... } |
| Call super | super(args) |
| Abstract class | abstract class Shape { ... } |
| Implement interface | class Dog implements Animal { ... } |
Async (Future / async-await)
Futures represent values available later; await pauses until they resolve.
| Operation | Syntax |
|---|---|
| Async function | Future<int> load() async { ... } |
| Await a future | var data = await load(); |
| Return a value | return 42; inside an async function |
| Delay | await Future.delayed(Duration(seconds: 1)); |
| Handle errors | try { await load(); } catch (e) { ... } |
| Then chaining | load().then((v) => print(v)); |
| Run in parallel | await Future.wait([a(), b()]); |
| Async stream | await for (var x in stream) { ... } |
The Dart syntax you reach for most, on one page. This Dart cheat sheet is a quick reference for the core language - variables and types, null safety, strings, collections, control flow, functions, classes, and the futures and async/await you use to write the apps behind Flutter.
Everything here is standard Dart and runs on the official SDK. Copy what you need, or try every snippet live in the Dart playground - no install required.
Dart cheat sheet FAQ
Is this Dart cheat sheet free?
How does null safety work in Dart?
int count can never hold null. To allow null you add a ? (String? name), and the compiler then forces you to handle the null case. Helpers make this concise: ?. for null-aware access, ?? for a fallback value, and ! to assert a value is non-null when you are certain. This catches null errors at compile time instead of at runtime.What is a Future and how do async and await work?
Future represents a value that will be available later, such as the result of a network call. Marking a function async lets you use await, which pauses execution until the future completes and then gives you the value - so you write asynchronous code that reads top to bottom like synchronous code. Wrap awaits in try/catch to handle errors.