There are several state management approaches, including Redux, MobX, bloc, flutter bloc, provider, and cubit. In flutter, state management is like a life support system in which the your entire App depends. Cubit is a simple management solution system.
It is part of the bloc package that emits
new states via methods rather than depending on events.
A Cubit
is class which extends BlocBase
and can be extended to manage any type of state. Each and every cubit
needs an initial state, which is the state of the cubit
prior to the call to emit
. The state
getter may be used to get a cubit
‘s current status.
class CounterCubit extends Cubit<int> {
CounterCubit() : super(0);
void increment() => emit(state + 1);
}
The cubit
state management can now be acquired and used through the Bloc package of Flutter. Since the migration of the bloc package, you just state the bloc package in your pubspec.yaml, cubit can now be accessed and used.
In your pubspec.yaml, add the pub dev package to your dependencies.
If Dart application, add the bloc
package to you pubspec.yaml as dependency.
dependencies:
bloc: ^8.0.0
if you are using it for Flutter applications, add the flutter_bloc
package to you pubspec.yaml as dependency.
dependencies:
flutter_bloc: ^8.1.1
For better understanding in utilizing the cubit
state management, refer to the bloc website.
But, if you prefer in using the cubit
independently, you can add the flutter_cubit
to the dependency in your pubspec.yaml. You may want to checkout the flutter dev website for reference.
dependencies:
flutter_cubit:
import the package in your dart file and implement by creating class and extending it to cubit
, given here in the example.
import 'package:cubit/cubit.dart';
class CounterCubit extends Cubit<int> {
CounterCubit() : super(1);
void increment() {
emit(state + 1);
}
void decrement() {
if (state > 1) emit(state - 1);
}
}
That’s it. Hoping this assist you in “What Is Cubit In Flutter?“. Thank you!