Irbisa · Cheatsheet
Dart Basics
Variables, types, functions, null safety, and core Dart syntax
What are the built-in types in Dart, and what does "everything is an object" mean?
What is the difference between
var,dynamicandObject?in Dart?void main() { var name = 'Alice'; // inferred as String // name = 42; // compile error dynamic value = 'Alice'; value = 42; // allowed: no static type…What is the difference between
finalandconstin Dart?final startedAt = DateTime.now(); // runtime value: fine for final// const now = DateTime.now(); // compile error: not a constant const maxItems = 100;const colors = ['red', 'green']; // deeply immutable…What is sound null safety in Dart, and how do nullable and non-nullable types differ?
void show(String? email) { // may be null // print(email.length); // compile error: email may be null if (email != null) { print(email.length); // promoted to String inside the if }}…What do the null-aware operators
?.,??,??=,?..,...?and the!operator do?class User { final String? nickname; User(this.nickname);} String label(User? user) =>…What do
is,is!andasdo in Dart, and why canascrash at runtime whileiscannot?double priceOf(Map<String, dynamic> json) { // wrong: throws when the server sends 10 instead of 10.0 // return json['price'] as double; // right: accept int or double, convert once return (json['price'] as num).toDouble();…What is the difference between positional, named and optional parameters in Dart, and what does
requireddo?int add(int a, int b) => a + b; // positional, required // optional positional: in [ ], must come lastString log(String msg, [String level = 'INFO']) => '[$level] $msg'; // named: in { }, any order at the call site…What are arrow functions, anonymous functions and closures in Dart?
int twice(int x) => x * 2; // named function, arrow body // closure: the returned function keeps `count` aliveint Function() makeCounter() { var count = 0; return () => ++count;…What is a
typedefin Dart, and how do you declare a function type?// a function type written inlinevoid run(int Function(int, int) op) => print(op(2, 3)); // the same idea with namestypedef BinaryOp = int Function(int a, int b);typedef Json = Map<String, dynamic>; // alias for any type…What can a Dart
enumhold since Dart 2.17 (fields, aconstconstructor, methods,implements), and how do you turn one into aStringand back?enum Plan implements Comparable<Plan> { free(priceCents: 0, maxProjects: 3), pro(priceCents: 999, maxProjects: 50), team(priceCents: 2999, maxProjects: 500); const Plan({required this.priceCents, required this.maxProjects});…What is the difference between
List,SetandMap, and which collection methods should every Dart developer know?class User { final int id; final String name; const User(this.id, this.name);}…How do the cascade
.., the spread...and collectionif/forwork in Dart?class Request { String method = 'GET'; final headers = <String, String>{}; void send() => print('$method $headers');}…What does this code print, and how does Dart pass objects to variables and functions?
void reassign(List<int> items) { items = [99];} void mutate(List<int> items) { items.add(4);} void main() { final a = [1, 2, 3]; final b = a; b.add(0); reassign(a); mutate(b); print(a);}If each of lines A–E ran on its own, which would throw at runtime, and what kind of list is each variable?
void main() { final a = [1, 2]; a.add(3); // line A const b = [1, 2]; b.add(3); // line B final c = List.unmodifiable(a); c[0] = 9; // line C final d = List.filled(2, 0); d[0] = 9; // line D d.add(1); // line E}Find the bug: placing one
Xon this board puts anXin every row.class Board { Board(this.size) : cells = List.filled(size, List.filled(size, '.')); final int size; final List<List<String>> cells; void place(int row, int col, String mark) => cells[row][col] = mark; String render() => cells.map((row) => row.join()).join('\n');} void main() { final board = Board(3)..place(0, 1, 'X'); print(board.render());}How does error handling work in Dart, and what is the difference between an
Exceptionand anError?class PaymentDeclined implements Exception { final String code; const PaymentDeclined(this.code); @override String toString() => 'PaymentDeclined($code)';}…What is
assertfor in Dart, and why must production validation usethrowand debug-only branches usekDebugMode?import 'package:flutter/foundation.dart'; class Badge { final int count; // our own invariant: checked in debug, gone in release const Badge(this.count) : assert(count >= 0, 'count must be >= 0');…What does this code print, and why do the
forloop and thewhileloop behave differently?void main() { final callbacks = <void Function()>[]; for (var i = 0; i < 3; i++) { callbacks.add(() => print(i)); } var j = 0; while (j < 3) { callbacks.add(() => print(j)); j++; } for (final cb in callbacks) cb();}Find the bug: this controller sometimes crashes with "Null check operator used on a null value" in
refreshAvatar.class ProfileController { ProfileController(this.api); final AvatarApi api; User? user; // cleared on logout Future<void> refreshAvatar() async { if (user == null) return; final url = await api.fetchAvatarUrl(user!.id); user!.avatarUrl = url; } void logout() => user = null;}What is the difference between
==andidentical()in Dart, and how doesconstcanonicalization affect it?class Point { final int x, y; const Point(this.x, this.y);} class Money {…Why is
0.1 + 0.2 == 0.3false in Dart, and how should you store and calculate money?String formatCents(int cents) => '${cents ~/ 100}.${(cents % 100).toString().padLeft(2, '0')}'; void main() { print(0.1 + 0.2); // 0.30000000000000004 print(0.1 + 0.2 == 0.3); // false…How do UTC and local
DateTimevalues differ in Dart, and how do you store and compare dates so users abroad see the right time?void main() { // wrong: no Z, parsed as LOCAL, so the instant differs per device final a = DateTime.parse('2026-03-29T10:00:00'); // right: explicit UTC, the same instant everywhere final b = DateTime.parse('2026-03-29T10:00:00Z');…