BlocObserver

open class BlocObserver

Global observer that receives lifecycle notifications from every Bloc and Cubit in the app.

Set a custom subclass once at app startup, before any Blocs are created:

class App : Application() {
override fun onCreate() {
super.onCreate()
BlocObserver.shared = AppBlocObserver()
}
}

Hook summary

HookWhen it firesEmitter type
onCreateEmitter is initialisedBloc + Cubit
onEventBefore an event is dispatchedBloc only
onChangeAfter every emit()Bloc + Cubit
onTransitionAfter a synchronous emit() with event contextBloc only
onErrorWhen addError() is calledBloc + Cubit
onCloseWhen close() is calledBloc + Cubit

Always call super in every override so the chain remains intact as the library grows.

Constructors

Link copied to clipboard
constructor()

Types

Link copied to clipboard
object Companion

Functions

Link copied to clipboard
open fun <S : Any> onChange(emitter: StateEmitter<S>, change: Change<S>)

Called after every Cubit.emit / Bloc.emit, with a Change containing the previous and next states.

Link copied to clipboard
open fun <S : Any> onClose(emitter: StateEmitter<S>)

Called when Cubit.close / Bloc.close completes.

Link copied to clipboard
open fun <S : Any> onCreate(emitter: StateEmitter<S>)

Called immediately after a Cubit or Bloc is constructed.

Link copied to clipboard
open fun <S : Any> onError(emitter: StateEmitter<S>, error: Throwable)

Called whenever Cubit.addError / Bloc.addError is invoked.

Link copied to clipboard
open fun <S : Any, E : Any> onEvent(bloc: Bloc<S, E>, event: E)

Called immediately before an event is dispatched to its handler. Only fires for Bloc subclasses, not Cubit.

Link copied to clipboard
open fun <S : Any, E : Any> onTransition(bloc: Bloc<S, E>, transition: Transition<E, S>)

Called after a synchronous Bloc.emit that was triggered by an event handler. Async emissions (those inside a launched coroutine) reach onChange but not here.