Sub topic 1
Why Immutable Data Classes
Prerequisites
This chapter assumes you know Dart classes, constructors, named parameters, and null safety. You should also understand the basic structure of a Flutter app and know how a widget receives and displays state. No knowledge of freezed is required yet.
What immutability means
An immutable object is an object whose values cannot change after it is created. To represent a change, you create a new object with the updated values.
A mutable object changes internally:
cart.items.add('Book');Code elsewhere may still hold a reference to cart. That code now sees a different cart, even though no new value was passed to it. This hidden change makes state harder to trace.
With immutable data, the old value remains available and the new value is explicit:
class CartState {
CartState._(this.items);
factory CartState({required List<String> items}) {
return CartState._(List.unmodifiable(items));
}
final List<String> items;
CartState add(String item) {
return CartState(items: [...items, item]);
}
}
void main() {
final emptyCart = CartState(items: []);
final cartWithBook = emptyCart.add('Book');
print(emptyCart.items); // []
print(cartWithBook.items); // [Book]
}List.unmodifiable prevents callers from changing the list through the items field. The add method creates a new list and a new CartState.
Why this helps Flutter
Flutter updates the screen when state changes cause widgets to rebuild. Immutable state gives each version a clear boundary:
- The application starts with one state object.
- An event, such as a button tap, produces a new state object.
- The state holder publishes that new object.
- Widgets rebuild from the new value.
This flow makes it easier to answer questions such as “Which action changed this value?” and “What was the previous state?”
Immutable state also reduces accidental sharing. If two widgets receive the same object, one widget cannot silently modify the object for the other widget. A change must travel through the code that creates and publishes the replacement value.
Identity and value
Dart objects have identity. Two separately created objects are usually different objects, even when their fields contain the same data.
final first = {'name': 'Ada'};
final second = {'name': 'Ada'};
print(identical(first, second)); // falseState code often needs value equality instead. Value equality means that two objects are considered equal when their meaningful fields match. This makes comparisons, tests, caching, and rebuild decisions easier to reason about.
Writing value equality by hand requires ==, hashCode, and usually toString. Data classes such as those generated by freezed provide this repeated behavior consistently.
The state transition model
The key idea is to treat a state change as a transition from one complete value to another, rather than as a mutation hidden inside an existing object.
This model supports predictable debugging. You can log the old state and the new state, compare them, and reproduce the same transition with the same input.
Exercise
A screen has a shared User object. A profile widget changes the user's display name by mutating that object directly. What is the safer immutable approach?
Show answer
Create a new User value with the updated display name, then publish that replacement through the application's state mechanism. Keep the original User unchanged.