Bloc Library

The koin_bloc package is dedicated to bring Flutter Bloc library features.


The koin_bloc makes it easy to use koin with Bloc library.

Koin Bloc API

The koin_bloc.dart introduces API definition keywords that comes in complement of single and scoped, to help declare a Bloc or Cubit component.

  • cubit definition, create an single and close the object at the end of the global koin context lifetime.
  • scopedCubit definition, create an object scoped and close when the associated scope is closed(Lifetime end).


Declaring a bloc component means that Koin container will keep a unique instance and close when stopKoin is called.

Declare you single Cubit:

// Declare bloc instance
// The 'dispose' metohod will be called at the end of the koin life time.
final myModule = module()..bloc((s) => MyCubit());


To declare a scoped cubit definition, use the scopedCubit function like follow. The scoped instance will be automatically closed when the scope is closed.

// Declare scopedCubit instance.
// The 'close' metohod will be called at the end of the scope life time.
scope.scopedCubit((s) => LoginBloc());


Example of use:

class CounterCubit extends Cubit<int> {
CounterCubit() : super(0);
void increment() => emit(state + 1);
scope.scopedCubit((s) => CounterCubit());


Example of use:

enum CounterEvent { increment, decrement }
class CounterBloc extends Bloc<CounterEvent, int>{
CounterBloc() : super(0);
Stream<int> mapEventToState(CounterEvent event) async* {
// ...
scope.scopedCubit((s) => CounterBloc());