Step 1 / 5
Fields that never change
Marking fields final makes the object immutable: once created, it can't be edited.
Flutter leans on this pattern everywhere. Widgets themselves are immutable.
Model data with immutable classes, constructor shorthands, named parameters and arrow functions.
1class Todo {2 final String title;3 final bool done;45 const Todo(this.title, {this.done = false});67 Todo toggle() => Todo(title, done: !done);89 @override10 String toString() => '[${done ? 'x' : ' '}] $title';11}1213void main() {14 final todos = [const Todo('Learn Dart'), const Todo('Ship app')];15 final updated = todos.map((t) => t.toggle()).toList();16 updated.forEach(print);17}
Marking fields final makes the object immutable: once created, it can't be edited.
Flutter leans on this pattern everywhere. Widgets themselves are immutable.