Quick Reference Dart Cheatsheet
Dart 3 on one page — null safety, types, operators, control flow, functions, collections,
classes and mixins, records and sealed types, Futures, and the traps that show up in
Flutter interviews.
10 sections 9 tables 20 code snippets Smallest program void main() {
const name = "Ravi";
print("Hello, $name");
} Create, run, check dart create hello
dart run
dart analyze
dart format .
dart test Dart is null-safe. A String cannot hold null. A String? can. Flutter UI is Dart, so these rules apply in every widget. Common types int n = 42;
double price = 19.5;
num either = 3;
bool ok = true;
String title = "Dart";
String? bio;
Object box = title; Null helpers String label = bio ?? "guest";
int? len = bio?.length;
bio ??= "guest";
String sure = bio!; Checks if (value is String) {
print(value.length);
}
if (value is! int) {
print("not an int");
} Cascade writes the same object final buffer = StringBuffer()
..write("Hello")
..write(" Dart");
print(buffer.toString()); 🔀 Control Flow & Patterns if and for String grade;
if (score >= 90) {
grade = "A";
} else if (score >= 80) {
grade = "B";
} else {
grade = "C";
}
for (final tag in tags) {
print(tag);
} Switch expression final label = switch (status) {
Status.draft => "Draft",
Status.live => "Live",
}; Dart 3 switch statements do not fall through. You do not write break after every case. Named and positional String greet(String name, {String title = "Hi"}) {
return "$title, $name";
}
int add(int a, int b) => a + b;
void log(String message, [String? tag]) {
print(tag ?? message);
} Required named argument User create({required String name, int age = 0}) {
return User(name, age: age);
}
final user = create(name: "Ravi"); Create and update final tags = <String>["dart", "flutter"];
tags.add("web");
final user = <String, String>{
"name": "Ravi",
"city": "Hyderabad",
};
print(user["name"]); Transform final doubled = nums.map((n) => n * 2).toList();
final adults = users.where((u) => u.age >= 18).toList();
final names = users.map((u) => u.name).toSet(); list1 == list2 compares identity, not contents. Two lists with the same items are not equal unless you compare them yourself. 🧱 Classes, Mixins & Enums Initializing formals class User {
User(this.name, {required this.age});
final String name;
final int age;
String greet() => "Hi, $name";
} Mixin and enum mixin Id {
String id = "x";
}
class Post with Id {
Post(this.title);
final String title;
}
enum Status { draft, live } A record (int, String) pair = (21, "Ravi");
final point = (x: 1, y: 2);
print(point.x); Sealed hierarchy sealed class Shape {}
class Circle extends Shape {
Circle(this.radius);
final double radius;
}
class Rect extends Shape {
Rect(this.width, this.height);
final double width;
final double height;
} Records are values. Two records with the same fields are equal. A sealed class is the Dart way to get an exhaustive switch: add a subclass and the compiler shows every switch you forgot. await a Future Future<String> load() async {
return "ok";
}
Future<void> run() async {
final text = await load();
print(text);
} Handle failure try {
final text = await load();
print(text);
} catch (error, stack) {
print(error);
} finally {
print("done");
} Forgetting await starts the work and drops the result and the error. An unawaited Future that throws becomes an unhandled async error. final is not deep final tags = <String>["dart"];
tags.add("flutter");
tags = <String>[]; Check, then use void show(String? name) {
if (name == null) return;
print(name.length);
} How to use this page: the last line of the final sample does not compile. That is the point. Type a sealed class with two subclasses and a switch, then add a third subclass and let the analyzer find the missing case. |