Immutable bloc state versus mutable ChangeNotifier
reasoning about state mutability trade-offs.
immutable gives predictable diffs, easy debugging and replayability at the cost of boilerplate; mutable is terse but error-prone.
declaring one universally superior with no nuance.
WHAT THIS TESTS Whether you can articulate engineering trade-offs rather than parrot a favorite library, covering predictability, debugging, performance and developer effort.
A GOOD ANSWER COVERS Immutable patterns emit a fresh state object on every change. Combined with freezed you get value equality, copyWith, and union types for loading or error states. Benefits include predictable transitions, straightforward testing of input to output, safe concurrent reads, and tooling like bloc observers for time-travel debugging. The cost is boilerplate and a code-generation step. Mutable ChangeNotifier mutates fields directly and calls notifyListeners. It is concise and has low ceremony, good for small or local state, but it is easy to forget to notify, to mutate a list in place without triggering a rebuild, or to leak a notifier.
COMMON WRONG ANSWERS Saying immutable is always faster; allocating new objects can add pressure, and rebuild scope matters more. Saying ChangeNotifier cannot scale; it can with discipline. Conflating mutability with performance instead of with safety and clarity.
LIKELY FOLLOW-UPS How does freezed equality reduce unnecessary rebuilds. How do you debug a missed notifyListeners. When would you mix both. How does Selector or buildWhen narrow rebuilds.
ONE CONCRETE EXAMPLE A checkout flow with loading, success and error branches is natural as a freezed sealed state; the UI switches on the variant and tests assert each emitted state in order. A simple expand-collapse toggle on a settings tile is overkill for bloc; a ChangeNotifier or even local setState is clearer. The senior move is matching ceremony to the state's complexity and lifetime, not picking one dogmatically.
Read the original → resocoder.com
Get five bites like this every day.
Tezvyn delivers a daily feed of 60-second tech bites with quizzes to lock in what you learn.