State ManagementIntermediate30 XP3 min read
What is the difference between mutable ChangeNotifier and immutable state?
TL;DR: ChangeNotifier holds mutable state and calls notifyListeners(). Immutable state (e.g., with freezed) creates new state objects on change — enabling value equality, easier debugging, and predictable rebuilds.
Full Answer
| Aspect | Mutable ChangeNotifier | Immutable State |
|---|---|---|
| State mutations | Mutate fields in-place | Create new state objects (copyWith) |
| Equality | Reference equality — hard to detect field changes | Value equality — == compares all fields |
| Debugging | Hard to trace what changed | Each emission is a snapshot — easy to diff |
| Rebuild control | notifyListeners() rebuilds all listeners | Equality checks skip unnecessary rebuilds |
| Boilerplate | Low | Higher (use freezed/equatable to reduce it) |
🎯
Use the freezed package to generate copyWith, ==, hashCode, and toString for immutable state classes automatically.
Code Examples
dartImmutable state with freezed
Output
current.isLoading == false; updated.isLoading == true; current == updated → false
Common Mistakes
- ✗Mutating a list inside ChangeNotifier without calling notifyListeners()
- ✗Comparing ChangeNotifier instances with == expecting value equality
Interview Tip
💡
Mentioning freezed as a code-generation tool for immutable state shows you're aware of Dart's ecosystem beyond the standard library.
#ChangeNotifier#immutable#state#equatable#freezed