Cubit

Pub build coverage Star on GitHub Discord License: MIT Starware

Cubit is a lightweight state management solution. It is a subset of the bloc package that does not rely on events and instead uses methods to emit new states.

Every cubit requires an initial state which will be the state of the cubit before emit has been called. The current state of a cubit can be accessed via the state getter.

Creating a Cubit

class CounterCubit extends Cubit<int> {
  CounterCubit() : super(0);

  void increment() => emit(state + 1);
}

Using a Cubit

void main() async {
  final cubit = CounterCubit()..increment();
  await cubit.close();
}

Observing a Cubit

onTransition can be overridden to observe state changes for a single cubit.

class CounterCubit extends Cubit<int> {
  CounterCubit() : super(0);

  void increment() => emit(state + 1);

  @override
  void onTransition(Transition<int> transition) {
    print(transition);
    super.onTransition(transition);
  }
}

CubitObserver can be used to observe state changes for all cubits.

class MyCubitObserver extends CubitObserver {
  @override
  void onTransition(Cubit cubit, Transition transition) {
    print(transition);
    super.onTransition(cubit, transition);
  }
}
void main() {
  Cubit.observer = MyCubitObserver();
  // Use cubits...
}

Dart Versions

  • Dart 2: >= 2.7.0

Maintainers

Supporters

Very Good Ventures

Starware

Cubit is Starware.
This means you're free to use the project, as long as you star its GitHub repository.
Your appreciation makes us grow and glow up. ⭐

Libraries

cubit