Getting everything ready for you…

Loading courses, problems and your progress. This takes just a moment.

0s

Dart Cheat Sheet

Basics

void main() { print('Hi $name ${a + b}'); }
var x = 5; final y = 10; const z = 15; String? maybe;
int.parse('42'); double.parse('2.5'); 42.toString(); 7 ~/ 2;
maybe ?? 'default'; maybe?.length; value!;

Control flow

if (a) {} else if (b) {} else {}
for (var i = 0; i < n; i++) {} for (final x in list) {}
while (cond) {} do {} while (cond);
var r = switch (x) { 1 => 'one', _ => 'other' };

Collections

var l = [1, 2]; l.add(3); l.remove(1); l.length; l.contains(2);
l.where((x) => x > 1).map((x) => x * 2).toList(); l.reduce((a, b) => a + b);
var m = {'a': 1}; m['b'] = 2; m.keys; m.values; m.containsKey('a'); m.forEach((k, v) {});
var s = {1, 2}; s.add(3); [...l, 4]; [for (var x in l) x * 2];

Functions and classes

int add(int a, int b) => a + b;
void f({required String name, int age = 18}) {}
class P { final int x; P(this.x); P.zero() : x = 0; }
class Q extends P with M implements I { Q() : super(1); @override String toString() => 'Q'; }

Async

Future<int> load() async { await Future.delayed(Duration(seconds: 1)); return 1; }
final v = await load(); await Future.wait([a(), b()]);
Stream<int> s() async* { yield 1; } await for (final x in s()) {}
try { await load(); } catch (e) { print(e); } finally {}

Flutter CLI

// create, run, build
flutter create my_app
flutter run
flutter pub add http
flutter build apk

Full Dart documentation