Menu

Dart Cheat Sheet

Last updated

Hello World & program structure

Every Dart program starts at the top-level main function.

OperationSyntax
Entry pointvoid main() { ... }
Print a lineprint("Hello, World!");
String interpolationprint("Hi $name");
Expression in interpolationprint("Sum: ${a + b}");
Comment// this is a comment
Multi-line comment/* ... */
Import a libraryimport 'dart:math';
Run a filedart run main.dart

Variables & types

Dart is statically typed but can infer types with var.

OperationSyntax
Inferred variablevar age = 30;
Explicit typeint age = 30;
Compile-time constantconst pi = 3.14;
Runtime constantfinal name = getName();
Basic typesint, double, String, bool
Dynamic typedynamic x = 5;
Type conversionint.parse("42"), n.toString()
Check typex is String, x as String

Null safety

Types are non-nullable by default; add ? to allow null.

OperationSyntax
Non-nullable (default)int count = 0;
Nullable typeString? name;
Null-aware accessuser?.name
Null-coalescingname ?? "default"
Null-coalescing assignname ??= "default";
Assert non-nullname!
Late initializationlate String value;
Null-aware spread[...?maybeList]

Strings

Strings support single, double, and triple quotes.

OperationSyntax
Lengths.length
Uppercase / lowercases.toUpperCase(), s.toLowerCase()
Interpolation"Total: $price"
Concatenate"foo" + "bar"
Containss.contains("ell")
Starts / ends withs.startsWith("he")
Split"a,b,c".split(",")
Replaces.replaceAll("a", "b")
Substrings.substring(0, 3)
Trims.trim()

Collections (List, Map, Set)

Three core collection types with literal syntax.

OperationSyntax
List literalvar nums = [1, 2, 3];
Add to listnums.add(4);
Access / lengthnums[0], nums.length
Map / wherenums.map((n) => n * 2), nums.where((n) => n > 1)
Map literalvar ages = {"Ada": 30};
Map accessages["Ada"]
Set literalvar ids = {1, 2, 3};
Spread operatorvar all = [...a, ...b];
Collection if / for[if (show) 1, for (n in xs) n]

Control flow

Conditions go in parentheses; switch supports patterns.

OperationSyntax
If / elseif (x > 0) { ... } else { ... }
Ternaryvar r = x > 0 ? "pos" : "neg";
Switchswitch (n) { case 1: ...; default: ... }
For loopfor (var i = 0; i < 10; i++) { ... }
For-in loopfor (var item in items) { ... }
forEachitems.forEach((x) => print(x));
While loopwhile (x < 100) { ... }
Do-whiledo { ... } while (x < 100);
Break / continuebreak;, continue;

Functions

Functions are first-class; arrow syntax shortens single expressions.

OperationSyntax
Define a functionint add(int a, int b) { return a + b; }
Arrow functionint square(int x) => x * x;
Optional positionalvoid log(String m, [int? code]) { ... }
Named parametersvoid box({int w = 0, int h = 0}) { ... }
Required namedvoid box({required int w}) { ... }
Anonymous functionvar f = (x) => x * 2;
Pass as argumentnums.map((n) => n * 2)
Typedeftypedef IntOp = int Function(int);

Classes & constructors

Classes hold state and behavior; constructors come in several forms.

OperationSyntax
Define a classclass Point { int x; int y; }
ConstructorPoint(this.x, this.y);
Named constructorPoint.origin() : x = 0, y = 0;
Create instancevar p = Point(1, 2);
Methoddouble dist() { ... }
Getterint get area => w * h;
Inheritanceclass Circle extends Shape { ... }
Call supersuper(args)
Abstract classabstract class Shape { ... }
Implement interfaceclass Dog implements Animal { ... }

Async (Future / async-await)

Futures represent values available later; await pauses until they resolve.

OperationSyntax
Async functionFuture<int> load() async { ... }
Await a futurevar data = await load();
Return a valuereturn 42; inside an async function
Delayawait Future.delayed(Duration(seconds: 1));
Handle errorstry { await load(); } catch (e) { ... }
Then chainingload().then((v) => print(v));
Run in parallelawait Future.wait([a(), b()]);
Async streamawait 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?
Yes. This Dart cheat sheet is completely free, with no sign-up required. Bookmark it and come back whenever you need to look up a null-safety operator, collection method, or async pattern.
How does null safety work in Dart?
In Dart, every type is non-nullable by default - 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?
A 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.
Can I practice Dart online?
Yes. Open the Dart playground to run any snippet from this cheat sheet in your browser - no SDK to install. When you want structure, Coddy's free interactive Dart course takes you from null safety and collections to classes and async/await step by step.
Is this cheat sheet good for beginners?
Yes. It is organized from the most common topics (variables, null safety, control flow) down to advanced ones (classes, futures, async/await), so you can use the top sections on day one and grow into the rest.
Coddy programming languages illustration

Learn Dart with Coddy

GET STARTED