Final Fields and Deep Immutability
Build objects that never change.
Final Fields and Deep Immutability is a free Dart Academy lesson on CoddyKit — lesson 1 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Dart Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
Immutable by Design
An immutable object never changes after it is built. That makes your data predictable and safe to share across your whole app. 🔒
Meet final
A final field is set once, during construction, and can never be reassigned. It locks the value in place for the life of the object.
class Point {
final int x;
Point(this.x);
}Reassignment Is an Error
Try to assign a new value to a final field and the compiler stops you. The mistake is caught before your program ever runs.
final p = Point(3);
p.x = 9; // compile errorfinal vs const
Use const when the value is known at compile time, and final when it is computed at run time. Both forbid later reassignment.
const pi = 3.14;
final now = DateTime.now();const Constructors
Give a class a const constructor and Dart can build truly fixed instances. All its fields must themselves be final.
class Color {
final int rgb;
const Color(this.rgb);
}Shared const Instances
Two identical const objects are the very same instance in memory. Dart reuses them, which saves both space and time.
const a = Color(0);
const b = Color(0);
print(identical(a, b)); // trueShallow vs Deep
A final field stops reassignment, but it does not freeze what it points to. A final List can still have items added to it.
final nums = [1, 2];
nums.add(3); // allowed!The Deep Immutability Goal
Deep immutability means nothing reachable from the object can change either. You need more than final to reach it fully.
Freeze Collections
Wrap a list in List.unmodifiable so any attempt to add or remove throws. Now the whole structure stays frozen.
final tags = List.unmodifiable([1, 2]);
tags.add(3); // throws at runtimeconst Collection Literals
Prefix a literal with const to get a deeply immutable, compile-time collection. It cannot be modified at all.
const days = ['Mon', 'Tue'];
// days.add(...) is forbiddenWhy It Matters
Immutable data is thread-safe, easy to cache, and friendly to state management in Flutter. Bugs from sneaky mutation simply vanish.
Quick Check
One last check on final and immutability.
Recap
You learned that final locks a field, const locks compile-time values, and deep immutability needs unmodifiable or const collections. 🎉
Frequently asked questions
Is the “Final Fields and Deep Immutability” lesson free?
Yes — the full text of “Final Fields and Deep Immutability” is free to read here on the web, and the Dart Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Dart Academy course, upgrade to CoddyKit PRO.
What will I learn in “Final Fields and Deep Immutability”?
Build objects that never change. You practise Dart Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Dart Academy?
No prior experience is required. Dart Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 1 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Final Fields and Deep Immutability” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Dart Academy lesson?
Yes. Every Dart Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Final Fields and Deep Immutability
- sealed Classes and Exhaustive switch
- base, interface, and final Modifiers
- copyWith and Value Equality