Cubit exposes methods that emit states. Bloc receives events and maps them to states. Both encourage a visible state-transition boundary; neither automatically defines your repository or backend contract.
Start with a focused Cubit
Add flutter_bloc and import package:flutter_bloc/flutter_bloc.dart. The fragment defines a counter-like model and provides it above a builder. A real feature can replace the integer with an immutable state class containing data, status and errors.
Separate a rendered state from an effect
Use a builder to describe the visible interface. A one-time navigation or toast needs a deliberately controlled listener rather than a side effect in the widget build method.
Worked example
This is a focused code fragment. Place it in the class or file described above; it is not a complete standalone app.
class SessionCubit extends Cubit<int> {
SessionCubit() : super(0);
void complete() => emit(state + 1);
}
BlocProvider(create: (_) => SessionCubit(), child: Builder(builder: (context) =>
BlocBuilder<SessionCubit, int>(builder: (context, count) => FilledButton(
onPressed: () => context.read<SessionCubit>().complete(),
child: Text('$count sessions'),
)),
))
Read the result
The provider owns the Cubit it creates and closes it. With a directly created Cubit in a test or service, that creator must close it. Choose Bloc events when the event model and its processing rules help express the feature.
Model a transition, not only a notification
The Cubit method names the user intent, such as completing a session. Emitting a new value makes a state transition observable. For a larger feature, a state class can represent whether data is being loaded, refreshed or shown alongside a recoverable error.
The builder should describe the interface from that state. A toast or navigation event needs a deliberate listener/effect path so rendering the same state again does not repeat an unintended side effect.
Start with methods and states you can test in isolation. If an event-driven Bloc better describes ordering or concurrency, make that a response to the feature’s requirements. The state-management comparison connects this choice to ownership and asynchronous behavior.
Practice and verification
Describe states for loading a paginated list, including initial loading and a later page failure. Decide whether a method-based Cubit or event-based Bloc makes the intended transitions clearer.