Where a BLoC should live: three answers from a production Flutter app
App root, page or widget: how Third Wave Coffee's Flutter app decides where each bloc lives, why every home section loads on its own, and what each choice costs.
Every BLoC tutorial shows where to put the bloc for one screen. A real app asks harder questions. What happens when ten sections of one page load at once? When every row in a list needs its own state? When a bottom sheet needs the same cart as the page under it? This is how the Third Wave Coffee app answers them, and what each answer costs.
Start from the failure you want to avoid
The home page is a stack of sections the server chooses: it sends an ordered list of section names, and a name the app does not recognise renders nothing. The goal was simple to state: one slow or failed section must never hold up the page.
So each section gets its own bloc, starts its own request and owns its own loading and failure states:
@override
void initState() {
super.initState();
context.read<HomeBannerBloc>().add(LoadBanner());
}
@override
Widget build(BuildContext context) => BlocBuilder<HomeBannerBloc, BannerState>(
builder: (context, state) => switch (state) {
BannerLoading() => const BannerShimmer(),
BannerLoaded(:final items) => BannerCarousel(items: items),
_ => const SizedBox.shrink(), // failed: the section steps aside
},
);
A failed offers call removes the offers section and nothing else. Only a failed skeleton shows a full-page error. The cost is volume: ten section blocs means ten sets of files, and one request per section instead of one per page.
Three places to create a bloc
Where a bloc is created decides who can reach it and when it dies. The app uses three places:
| Where | Use it when | What it costs |
|---|---|---|
| App root | The page must coordinate it, or many screens read it | Lives as long as the app; the provider list grows past seventy |
| Page | The state belongs to one visit, like the wallet screen | Nothing outside the page can reach it |
| Widget | Many instances need different inputs, like product rows | It cannot be told anything; it has to listen |
The root is what lets the page coordinate: once the order-type bloc knows the selected store, the page tells the header, offers and menu sections to load for that store. The page level is for state that should start clean every visit: the wallet screen creates its blocs and closes them on the way out.
The widget level is the interesting one. A product row creates its own bloc where it is built, and because nothing above can reach it, it listens upward instead:
BlocProvider(
create: (_) => sl<ProductRowBloc>()..add(LoadRow(kind)),
child: BlocListener<CartBloc, CartState>(
listener: (context, state) {
if (state is CartCleared) context.read<ProductRowBloc>().add(LoadRow(kind));
},
child: const ProductRowView(),
),
)
When a card changes an item’s quantity, a small relay bloc at the root broadcasts it to every row on screen.
Shared state is a registration decision
Dependencies are registered by hand with get_it. Blocs are factories, so every provider gets a fresh one. The few that must be shared, the cart, the order type and the toolbar, are singletons, so a bottom sheet that asks for the cart gets the same one as the page underneath it. Making something shared is one line in a registration file, which is exactly why it deserves a second look in review.
The question I ask
For every new bloc: who needs to talk to it, and when should it die? Many readers and an app-length life means the root. One visit means the page. One instance among many means the widget. The rest of the app’s structure is in Feature architecture.