Cubit
A lightweight state-management class that exposes direct method calls instead of events.
Use Cubit when you don't need an event audit trail or EventTransformer strategies. It is shorter, easier to read, and equally testable.
class CounterCubit : Cubit<Int>(initialState = 0) {
fun increment() = emit(state + 1)
fun decrement() = emit(state - 1)
fun reset() = emit(0)
}Content copied to clipboard
Cubit vs Bloc
| Cubit | Bloc | |
|---|---|---|
| API style | Direct method calls | Dispatched events |
| Audit trail | No explicit event log | Full event history via Bloc.eventsFlow |
| Transformers | Not applicable | Debounce, Restartable, Droppable, … |
| Observer hooks | onCreate, onChange, onError, onClose | All of the above + onEvent, onTransition |
| Best for | Simple, well-understood state logic | Complex flows, analytics, rate-limiting |
Rule of thumb: Start with a Cubit. Upgrade to a Bloc when you need an event log or an EventTransformer strategy.
All methods are main-thread safe — the internal CoroutineScope runs on Dispatchers.Main. Override scope to change the dispatcher (e.g. for tests).