BLoC Pattern with Streams
Learn the BLoC (Business Logic Component) pattern in Flutter to separate business logic from UI using events, states and streams.
BLoC Pattern with Streams is a free Flutter Mobile Development lesson on CoddyKit — lesson 4 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Flutter Mobile Development learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
What Is BLoC?
BLoC stands for Business Logic Component. It is a pattern that keeps your business logic out of widgets by turning events into states through a stream.
- UI sends events in
- BLoC processes them
- New states flow back out to the UI
Why Another Pattern?
You already know setState, Provider, and Riverpod. BLoC shines when logic is complex and you want:
- Clear separation of UI and logic
- Predictable, testable state transitions
- A reactive stream-based flow
Streams Refresher
A Stream is a sequence of asynchronous values over time. A StreamController lets you push values and expose them as a stream.
final controller = StreamController<int>();
controller.stream.listen((value) => print(value));
controller.add(1);
controller.add(2);Events
An event is an immutable class describing something the user did. Define them as plain Dart classes.
abstract class CounterEvent {}
class Increment extends CounterEvent {}
class Decrement extends CounterEvent {}States
A state is an immutable snapshot of what the UI should show. Keep it simple and serializable.
class CounterState {
final int count;
const CounterState(this.count);
}The Bloc Class
Using the flutter_bloc package, a Bloc maps events to states with on<Event> handlers.
class CounterBloc extends Bloc<CounterEvent, CounterState> {
CounterBloc() : super(const CounterState(0)) {
on<Increment>((e, emit) => emit(CounterState(state.count + 1)));
on<Decrement>((e, emit) => emit(CounterState(state.count - 1)));
}
}Providing the Bloc
Wrap part of the tree in BlocProvider so descendants can access the bloc.
BlocProvider(
create: (_) => CounterBloc(),
child: const CounterPage(),
);Reacting with BlocBuilder
BlocBuilder rebuilds widgets whenever a new state is emitted.
BlocBuilder<CounterBloc, CounterState>(
builder: (context, state) => Text('Count: ' + state.count.toString()),
);Dispatching Events
Send events to the bloc with context.read<CounterBloc>().add(...).
FloatingActionButton(
onPressed: () => context.read<CounterBloc>().add(Increment()),
child: const Icon(Icons.add),
);BlocListener for Side Effects
Use BlocListener for one-time side effects like showing a SnackBar or navigating, where you do not want to rebuild UI.
BlocListener<CounterBloc, CounterState>(
listener: (context, state) {
if (state.count == 10) ScaffoldMessenger.of(context).showSnackBar(
const SnackBar(content: Text('Reached 10!')),
);
},
child: child,
);Cubit: A Lighter BLoC
A Cubit drops events and exposes methods directly that call emit. Use it when full event classes feel like overkill.
class CounterCubit extends Cubit<int> {
CounterCubit() : super(0);
void increment() => emit(state + 1);
}Quick Check
Which widget should you use to trigger a one-time SnackBar without rebuilding UI on every state?
Recap
You learned the BLoC pattern:
- Events in, states out, via streams
- Bloc with on<Event> handlers, or a lighter Cubit
- BlocProvider, BlocBuilder and BlocListener
BLoC gives you clean, testable separation of logic and UI for complex apps.
Frequently asked questions
Is the “BLoC Pattern with Streams” lesson free?
Yes — the full text of “BLoC Pattern with Streams” is free to read here on the web, and the Flutter Mobile Development course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Flutter Mobile Development course, upgrade to CoddyKit PRO.
What will I learn in “BLoC Pattern with Streams”?
Learn the BLoC (Business Logic Component) pattern in Flutter to separate business logic from UI using events, states and streams. You practise Flutter Mobile Development with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Flutter Mobile Development?
No prior experience is required. Flutter Mobile Development on CoddyKit is structured for beginners through advanced learners; this is — lesson 4 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “BLoC Pattern with Streams” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Flutter Mobile Development lesson?
Yes. Every Flutter Mobile Development lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- setState & InheritedWidget
- Provider Package Basics
- Riverpod for State
- BLoC Pattern with Streams