Complete the code to create a BLoC class that extends the correct base class.
class CounterBloc extends [1] { // BLoC logic here }
The Cubit<int> class is the base for simple BLoC classes managing integer state.
Complete the code to emit a new state value in the BLoC.
void increment() {
emit([1] + 1);
}The state property holds the current state value in a Cubit or BLoC.
Fix the error in the BLoC provider code to correctly provide the BLoC to the widget tree.
return BlocProvider( create: (context) => [1](), child: MyHomePage(), );
The create function must return an instance of the BLoC class, here CounterBloc().
Fill both blanks to build a widget that listens to BLoC state changes and rebuilds accordingly.
return BlocBuilder<CounterBloc, [1]>( builder: (context, [2]) { return Text('Count: $[2]'); }, );
The BlocBuilder listens to CounterBloc with state type int. The builder function receives the current state as second argument.
Fill both blanks to define a BLoC event class and handle it in the BLoC.
abstract class CounterEvent {} class IncrementEvent extends CounterEvent {} class CounterBloc extends Bloc<CounterEvent, int> { CounterBloc() : super(0) { on<[1]>((event, emit) { emit(state [2] 1); }); } }
The on<IncrementEvent> listens for the increment event. The state is updated by adding 1.