cubit 0.1.2

  • Readme
  • Changelog
  • Example
  • Installing
  • 99

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. ⭐

0.1.2 #

  • revert: v0.1.1

0.1.1 #

  • fix: offload state emission to next event loop iteration

0.1.0 #

  • docs: add onTransition documentation to README
  • docs: add CubitObserver documentation to README

0.0.14 #

  • feat: add CubitObserver support
  • docs: minor documentation improvements

0.0.13 #

  • docs: various documentation updates

0.0.12 #

  • docs: fix coverage badge

0.0.11 #

  • docs: various documentation updates

0.0.10 #

  • docs: various documentation updates

0.0.9 #

  • docs: documentation and logo updates

0.0.8 #

  • BREAKING: revert named initialState parameter (related issue)
  • docs: minor logo updates

0.0.7 #

  • feat: add onTransition to cubit
  • feat: Cubit extends CubitStream

0.0.6 #

  • BREAKING: ignore duplicate states

0.0.5 #

  • feat: allow initialState to be null

0.0.4 #

  • BREAKING: use named parameter for initialState

0.0.3 #

  • BREAKING: remove initialState getter and instead require initial state via constructor

0.0.2 #

  • BREAKING: update emit to be a void function
  • BREAKING: remove artifical wait to guarantee initialState is emitted
  • BREAKING: close drains the internal Stream
  • tests: 100% test coverage

0.0.1 #

  • feat: initial release

example/main.dart

import 'package:cubit/cubit.dart';

class MyCubitObserver extends CubitObserver {
  @override
  void onTransition(Cubit cubit, Transition transition) {
    print(transition);
    super.onTransition(cubit, transition);
  }
}

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

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

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

Use this package as a library

1. Depend on it

Add this to your package's pubspec.yaml file:


dependencies:
  cubit: ^0.1.2

2. Install it

You can install packages from the command line:

with pub:


$ pub get

with Flutter:


$ flutter pub get

Alternatively, your editor might support pub get or flutter pub get. Check the docs for your editor to learn more.

3. Import it

Now in your Dart code, you can use:


import 'package:cubit/cubit.dart';
  
Popularity:
Describes how popular the package is relative to other packages. [more]
98
Health:
Code health derived from static analysis. [more]
100
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
100
Overall:
Weighted score of the above. [more]
99
Learn more about scoring.

We analyzed this package on Jul 10, 2020, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.8.4
  • pana: 0.13.15

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.7.0 <3.0.0
meta ^1.1.8 1.2.2 1.3.0-nullsafety
Dev dependencies
mockito ^4.1.1
test ^1.14.6
test_coverage ^0.4.1