Dart Cheat Sheet
Dart is a modern, strongly typed programming language optimized for client development, especially for building cross-platform applications. This Dart cheatsheet is an example-driven reference covering core syntax, types, collections, object-oriented features, and asynchronous programming.
Basic Program Structure
void main() {
print('Hello, Dart');
}
Variables
var a = 10;
int b = 20;
final c = 30;
const d = 40;
var→ inferred typefinal→ set once at runtimeconst→ compile-time constant
Built-in Types
int x = 10;
double y = 3.14;
bool flag = true;
String text = 'hello';
num n1 = 10;
num n2 = 3.5;
Null Safety
int? value = null;
if (value != null) {
print(value);
}
int result = value ?? 0;
value ??= 5;
Strings
String s = 'Dart';
s.toUpperCase();
s.contains('a');
String name = 'World';
print('Hello $name');
print('2 + 2 = ${2 + 2}');
Operators
a + b;
a - b;
a * b;
a / b;
a ~/ b;
a % b;
a == b;
a != b;
a > b;
a < b;
a && b;
a || b;
!a;
Control Flow
if / else
if (x > 10) {
print('large');
} else {
print('small');
}
switch
switch (x) {
case 1:
break;
case 2:
break;
default:
break;
}
Loops
for (int i = 0; i < 5; i++) {
print(i);
}
while (x > 0) {
x--;
}
do {
x--;
} while (x > 0);
Functions
int add(int a, int b) {
return a + b;
}
int result = add(2, 3);
Arrow Functions
int square(int x) => x * x;
Optional Parameters
Positional
void log(String message, [String? tag]) {
print(tag ?? message);
}
Named
void connect({required String host, int port = 80}) {
}
connect(host: 'localhost', port: 8080);
Collections
List
List<int> numbers = [1, 2, 3];
numbers.add(4);
numbers[0];
Set
Set<int> values = {1, 2, 3};
values.add(4);
Map
Map<String, int> scores = {
'a': 1,
'b': 2,
};
Collection If / For
var list = [
1,
if (x > 2) 3,
for (var i in [4, 5]) i,
];
Iteration
for (var item in numbers) {
print(item);
}
numbers.forEach((n) {
print(n);
});
Classes
class Counter {
int value = 0;
void increment() {
value++;
}
}
var counter = Counter();
counter.increment();
Constructors
class Point {
final int x;
final int y;
Point(this.x, this.y);
}
var p = Point(10, 20);
Named Constructors
class User {
String name;
User(this.name);
User.guest() : name = 'guest';
}
Getters & Setters
class Box {
double _value = 0;
double get value => _value;
set value(double v) => _value = v;
}
Inheritance
class Animal {
void speak() {}
}
class Dog extends Animal {
}
Abstract Classes
abstract class Shape {
double area();
}
class Square extends Shape {
final double size;
Square(this.size);
double area() => size * size;
}
Interfaces (implements)
class Logger {
void log(String msg) {}
}
class ConsoleLogger implements Logger {
void log(String msg) {
print(msg);
}
}
Mixins
mixin Fly {
void fly() {}
}
class Bird with Fly {
}
Enums
enum Status { ok, error, loading }
Futures
Future<int> fetchValue() async {
await Future.delayed(Duration(seconds: 1));
return 42;
}
int v = await fetchValue();
Async / Await
void main() async {
var value = await fetchValue();
print(value);
}
Streams
Stream<int> countStream() async* {
for (int i = 0; i < 3; i++) {
yield i;
}
}
await for (var value in countStream()) {
print(value);
}
Exception Handling
try {
int.parse('x');
} catch (e) {
print(e);
} finally {
}
Typedefs
typedef IntOperation = int Function(int a, int b);
IntOperation sum = (a, b) => a + b;
Generics
class Box<T> {
T value;
Box(this.value);
}
var box = Box<int>(10);
File I/O (Dart VM)
import 'dart:io';
File file = File('data.txt');
file.writeAsStringSync('text');
String text = file.readAsStringSync();
JSON
import 'dart:convert';
var data = {'a': 1};
String encoded = jsonEncode(data);
var decoded = jsonDecode(encoded);