Skip to content
Home
Cheat Sheets
Dart
Dart Cheat Sheet
Learn Dart from the basics →
Variables & Types
var name = 'Ada';
Type inferred (String)
final city = 'Oslo';
Set once, at runtime
const maxUsers = 100;
Compile-time constant
int, double, num, String, bool
Core types
int.parse('42') / int.tryParse('x')
Convert — throwing vs null-returning
Strings
'Hello, $name! Next year: ${age + 1}'
String interpolation
s.length / s.isEmpty
Length / emptiness
s.toUpperCase() / s.trim()
Change case / trim
s.split(',') / s.contains('art')
Split / substring check
final text = ''' ... ''';
Multi-line string (triple quotes)
Null Safety
String? email;
Nullable type (starts as null)
email?.length
Null-aware access
email ?? 'none'
Default if null
email ??= 'none';
Assign only if null
late final String token;
Non-null, initialized later
Control Flow
final label = age >= 18 ? 'adult' : 'minor';
Conditional expression
switch (n) { 1 || 2 => 'low', >= 3 && <= 9 => 'mid', _ => 'high' }
Switch expression with patterns
for (var i = 0; i < 5; i++) { }
Counting loop
for (final item in items) { }
For-in loop
while (cond) { } / do { } while (cond);
While loops
Collections
final nums = [1, 2, 3];
List
final ages = {'ada': 36};
Map
final tags = {'dart', 'flutter'};
Set
nums.map((n) => n * 2).where((n) => n > 2).toList()
Transform and filter
[...a, if (show) 4, for (final x in b) x * 2]
Spread, collection if and for
Functions
int square(int n) => n * n;
Arrow function
void greet({required String name, String greeting = 'Hi'})
Named parameters
String join(String a, [String? b])
Optional positional parameter
final double = (int n) => n * 2;
Anonymous function
extension on String { String get shout => '${toUpperCase()}!'; }
Extension
Classes & Types
class Point { final int x, y; const Point(this.x, this.y); }
Class with a constant constructor
Point.origin() : x = 0, y = 0;
Named constructor with initializer list
class Dog extends Animal with Swimmer implements Pet
Inherit, mix in, implement
enum Size { small, medium, large }
Enum
sealed class Shape {} / final class Circle extends Shape {}
Sealed hierarchy for exhaustive switch
final (lat, lng) = (51.5, -0.1);
Record and destructuring
Async & Errors
Future<String> load() async { ... }
Async function
final data = await load();
Wait for a future
await Future.wait([a(), b()])
Run futures concurrently
await for (final v in stream) { }
Consume a stream
try { ... } on FormatException catch (e) { ... } finally { ... }
Handle exceptions
Tooling
dart create my_app
New console project
dart run
Run the project
dart pub add http
Add a package
dart test / dart analyze / dart format .
Test, lint, format
dart compile exe bin/app.dart
Native executable
Cookies & your privacy
We use essential cookies to keep this site working and secure.
With your permission, we'd also like to use Google Analytics cookies to understand site traffic.
We don't use advertising or social media tracking cookies, and we honor Global Privacy Control (GPC) browser signals.
Learn more in our Privacy Policy .
Your browser is sending a GPC signal, so analytics stays off even if you accept.
Accept
Reject