Why is the Equatable package used in BLoC state management?
TL;DR: Equatable overrides == and hashCode based on props, enabling value equality for state objects. Without it, BLoC would emit duplicate states (same data, different object) causing unnecessary rebuilds.
Full Answer
Dart classes have reference equality by default โ two objects with the same data are not equal unless == is overridden. BLoC skips emitting if the new state equals the current state.
Without Equatable, every emit() triggers a rebuild even if the data is identical. With Equatable, BLoC compares states by value and skips rebuilds when nothing changed.
Alternatively, use freezed โ it generates == and copyWith. The choice between Equatable and freezed is mostly team preference; both solve the equality problem.
Code Examples
s1 == s2 โ true First emit: triggers rebuild. Second emit: skipped by BLoC (equal state).
Common Mistakes
- โForgetting to include all relevant fields in props โ states appear equal when they're not
- โIncluding mutable objects (like lists) in props without proper list equality
Interview Tip
Explain that Equatable is a performance optimization โ it prevents duplicate rebuilds when BLoC re-emits the same data.