Cubit – simplified BLoC
Cubit is a subset of the BLoC library and it is widely used where states are less complex. It has no events, and states are triggered using functions directly from the UI. This creates less complicated code for simpler user flows. Let's see how our counter example application looks with Cubit in place (you won't need to add any extra package other than flutter_bloc
for using Cubit).
Creating a counter example application using Cubit
Most of the things in Cubit are going to be the same as they were in BLoC. We will be using the same state class that we used in BLoC, which is CounterState
. We won't have any event class for Cubit – we will have a Cubit class. So, let's create a file named counter_cubit.dart
and add the following code:
import 'counter_state.dart'; import 'package:flutter_bloc/flutter_bloc.dart'; class CounterCubit extends Cubit<CounterState> { CounterCubit() : super...