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.
void main() {
print('Hello, world!');
}
main() function โ same entry-point idea as Go and Java.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
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
int age = 20;
if (age >= 18) {
print('Adult');
} else if (age >= 13) {
print('Teen');
} else {
print('Child');
}
for (int i = 0; i < 5; i++) {
print(i);
}
int n = 3;
while (n > 0) {
print(n);
n--;
}
class Dog {
String name;
int age;
Dog(this.name, this.age);
void bark() {
print('$name says woof!');
}
}
// var d = Dog('Rex', 3); d.bark();
Full runnable programs, compiled and run in a real Dart sandbox โ same workspace as the other cheat sheets.
void main() {
var nums = [4, 8, 15, 16, 23, 42];
int sum = nums.fold(0, (a, b) => a + b);
print(sum);
}
String reverseStr(String s) {
return String.fromCharCodes(s.runes.toList().reversed);
}
void main() {
print(reverseStr('dart'));
}
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);
}
}
}
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'));
}