๐ŸŽฏ

Dart Cheat Sheet

Google's client-optimized language, best known as the engine behind Flutter โ€” write one Dart codebase and ship native-feeling iOS, Android, web, and desktop apps from it. Copy-paste examples plus live Run/Submit exercises.

Jump to: Your first programVariablesNull safety if / elseLoopsClasses ๐Ÿš€ Practice problems

Your first program

void main() {
    print('Hello, world!');
}
๐Ÿ“ Every Dart program needs a top-level main() function โ€” same entry-point idea as Go and Java.

Variables

int age = 25;
String name = 'Sam';
double price = 19.99;
var city = 'NYC';    // var infers the type โ€” still statically typed
final pi = 3.14159; // final = set once, can't reassign

Null safety

Dart's type system tracks nullability directly, same idea as Kotlin's โ€” a value can only be null if its type says so.

String? middleName;   // the ? means "this can be null"
print(middleName ?? 'no middle name');   // ?? gives a fallback

if / else

int age = 20;
if (age >= 18) {
    print('Adult');
} else if (age >= 13) {
    print('Teen');
} else {
    print('Child');
}

Loops

for (int i = 0; i < 5; i++) {
    print(i);
}

int n = 3;
while (n > 0) {
    print(n);
    n--;
}

Classes

class Dog {
    String name;
    int age;

    Dog(this.name, this.age);

    void bark() {
        print('$name says woof!');
    }
}

// var d = Dog('Rex', 3); d.bark();

๐Ÿš€ Practice problems

Full runnable programs, compiled and run in a real Dart sandbox โ€” same workspace as the other cheat sheets.

Sum a list EASY

void main() {
    var nums = [4, 8, 15, 16, 23, 42];
    int sum = nums.fold(0, (a, b) => a + b);
    print(sum);
}

Reverse a string MEDIUM

String reverseStr(String s) {
    return String.fromCharCodes(s.runes.toList().reversed);
}

void main() {
    print(reverseStr('dart'));
}

FizzBuzz MEDIUM

void main() {
    for (int i = 1; i <= 15; i++) {
        if (i % 15 == 0) {
            print('FizzBuzz');
        } else if (i % 3 == 0) {
            print('Fizz');
        } else if (i % 5 == 0) {
            print('Buzz');
        } else {
            print(i);
        }
    }
}

Word frequency with a Map HARD

void main() {
    String text = 'the cat sat on the mat the cat ran';
    var counts = <String, int>{};
    for (var word in text.split(' ')) {
        counts[word] = (counts[word] ?? 0) + 1;
    }
    counts.forEach((word, n) => print('$word: $n'));
}