Bloc and Cubit State Management
What is Bloc?
Bloc stands for Business Logic Component. It keeps the logic of your app in separate classes that are independent of the UI. The widget sends something in, listens to what comes out, and draws the result. This makes big apps clean and easy to test.
| Concept | Meaning |
|---|---|
| State | What the UI shows right now |
| Event | Something that happened (button pressed), only in Bloc |
| Cubit | A simple version: call methods that emit new states |
| Bloc | Receives events and emits states |
Install
bash
flutter pub add flutter_bloc
Cubit: The Easy Way
dart
import 'package:flutter_bloc/flutter_bloc.dart';
class CounterCubit extends Cubit<int> {
CounterCubit() : super(0); // initial state
void increment() => emit(state + 1);
void decrement() => emit(state - 1);
}
dart
void main() {
runApp(
BlocProvider(
create: (_) => CounterCubit(),
child: const MaterialApp(home: CounterPage()),
),
);
}
class CounterPage extends StatelessWidget {
const CounterPage({super.key});
@override
Widget build(BuildContext context) {
return Scaffold(
body: Center(
child: BlocBuilder<CounterCubit, int>(
builder: (context, count) => Text('$count', style: const TextStyle(fontSize: 48)),
),
),
floatingActionButton: FloatingActionButton(
onPressed: () => context.read<CounterCubit>().increment(),
child: const Icon(Icons.add),
),
);
}
}
Bloc with Events and States
Use a Bloc when you want a clear record of what happened (events). Here is a login example.
dart
// Events
sealed class LoginEvent {}
class LoginSubmitted extends LoginEvent {
final String email;
final String password;
LoginSubmitted(this.email, this.password);
}
// States
sealed class LoginState {}
class LoginInitial extends LoginState {}
class LoginLoading extends LoginState {}
class LoginSuccess extends LoginState {}
class LoginFailure extends LoginState {
final String message;
LoginFailure(this.message);
}
// Bloc
class LoginBloc extends Bloc<LoginEvent, LoginState> {
LoginBloc() : super(LoginInitial()) {
on<LoginSubmitted>((event, emit) async {
emit(LoginLoading());
await Future.delayed(const Duration(seconds: 1)); // pretend API call
if (event.password == '123456') {
emit(LoginSuccess());
} else {
emit(LoginFailure('Wrong password'));
}
});
}
}
Using It in the UI
dart
BlocConsumer<LoginBloc, LoginState>(
listener: (context, state) {
// one-time actions: SnackBar, navigation
if (state is LoginFailure) {
ScaffoldMessenger.of(context).showSnackBar(SnackBar(content: Text(state.message)));
}
},
builder: (context, state) {
if (state is LoginLoading) return const CircularProgressIndicator();
return FilledButton(
onPressed: () => context.read<LoginBloc>().add(LoginSubmitted('a@b.com', '123456')),
child: const Text('Login'),
);
},
)
| Widget | Use |
|---|---|
BlocProvider | Create and provide the Bloc or Cubit |
BlocBuilder | Rebuild the UI from the state |
BlocListener | Do one-time actions such as navigation or messages |
BlocConsumer | Builder and listener together |
context.read<T>() | Call methods or add events |
Cubit or Bloc? Start with Cubit. It has less code. Move to Bloc when you need events, for example to debounce a search or to log every action.
Conclusion
Bloc separates logic from the screen. Cubits are the quick start, and Blocs with events suit larger and well-tested projects.
Quick Summary: Cubit emits states from methods, Bloc turns events into states, and BlocBuilder draws the UI from the state.

