Sub topic 4
Providing a Cubit with BlocProvider and reading it with context.read
This chapter assumes Dart classes, generics, null safety, Flutter widget trees, BuildContext, and basic Cubit usage. It also assumes the flutter_bloc package is in your project.
Why BlocProvider exists
A Cubit contains state and methods that change that state. Widgets need a way to find the correct Cubit without creating one inside every widget. BlocProvider places a Cubit in the widget tree and makes it available to all descendant widgets.
The create callback constructs the Cubit. By default, construction is lazy, so the callback runs when a descendant first requests the Cubit. BlocProvider also closes a Cubit that it created when the provider is removed from the tree.
Creating and reading a Cubit
The provider must be an ancestor of the widget that reads it. context.read<CounterCubit>() looks up the nearest matching provider and returns the Cubit instance.
read performs a one-time lookup. It does not subscribe the widget to future state changes. Use it for actions such as button presses.
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
class CounterCubit extends Cubit<int> {
CounterCubit() : super(0);
void increment() => emit(state + 1);
}
class CounterPage extends StatelessWidget {
const CounterPage({super.key});
@override
Widget build(BuildContext context) {
return BlocProvider(
create: (_) => CounterCubit(),
child: const CounterView(),
);
}
}
class CounterView extends StatelessWidget {
const CounterView({super.key});
@override
Widget build(BuildContext context) {
return Scaffold(
body: Center(
child: Text('${context.watch<CounterCubit>().state}'),
),
floatingActionButton: FloatingActionButton(
onPressed: () => context.read<CounterCubit>().increment(),
child: const Icon(Icons.add),
),
);
}
}Here, watch subscribes the widget to changes, while read only retrieves the instance. In larger views, prefer BlocBuilder or BlocSelector for explicit and narrower rebuilds.
Provider scope and ownership
A provider's position determines which widgets can access its Cubit. Put it above the smallest shared part of the tree that needs the state. Putting it above MaterialApp gives broad access, but it can make ownership and lifetime harder to understand.
Use BlocProvider.value when passing an existing Cubit to another route or subtree:
BlocProvider.value(
value: context.read<CounterCubit>(),
child: const DetailsPage(),
)Do not use BlocProvider.value to create a new Cubit. The value form does not automatically close the instance because another part of the application owns it.
> callout note > A ProviderNotFoundException usually means the BuildContext is above the BlocProvider, or the provider is in a different route subtree.
A practical rule
Use BlocProvider(create: ...) when the provider owns construction and disposal. Use context.read for commands. Use BlocBuilder, context.watch, or BlocSelector when the UI must react to state.
Exercise
Why does the button use context.read instead of context.watch?
Show answer
The button only needs to call increment. It does not need to rebuild when the count changes; the separate state-reading widget handles that.