EventSubscription<TSourceBloc extends JuiceBloc<BlocState>, TSourceEvent extends EventBase, TLocalEvent extends EventBase> class

Subscribes to specific events from another bloc and executes a local use case.

EventSubscription enables loosely-coupled bloc-to-bloc communication by allowing a bloc to react to specific events emitted by another bloc, without needing to know the source bloc's state structure.

Unlike RelayUseCaseBuilder which observes all state changes, EventSubscription filters for specific event types, resulting in cleaner, more targeted inter-bloc communication.

The toEvent transformer isolates coupling - the subscribing bloc only needs to know about the source event structure, not the source state.

Type Parameters:

  • TSourceBloc - The bloc type to subscribe to
  • TSourceEvent - The event type to listen for from the source bloc
  • TLocalEvent - The local event type to transform into and execute

Example:

class ProfileBloc extends JuiceBloc<ProfileState> {
  ProfileBloc() : super(ProfileState(), [
    // Subscribe to AuthBloc's LoginSuccessEvent
    () => EventSubscription<AuthBloc, LoginSuccessEvent, LoadProfileEvent>(
      toEvent: (e) => LoadProfileEvent(userId: e.userId),
      useCaseGenerator: () => LoadProfileUseCase(),
    ),
  ], []);
}
Implemented types

Constructors

EventSubscription({required TLocalEvent toEvent(TSourceEvent sourceEvent), required UseCaseGenerator useCaseGenerator, bool when(TSourceEvent event)?, Set<Type> statusTypes = const {UpdatingStatus}, Object? scope, BlocDependencyResolver? resolver})
Creates an EventSubscription.

Properties

eventType Type
The type of event this use case handles.
no setteroverride
generator UseCaseGenerator
Creates the use case instance that will handle events.
no setteroverride
hashCode int
The hash code for this object.
no setterinherited
initialEventBuilder UseCaseEventBuilder?
Optional builder for creating an initial event when the bloc starts.
no setteroverride
runtimeType Type
A representation of the runtime type of the object.
no setterinherited
scope Object?
Optional scope key for resolving scoped bloc instances.
final
sourceBlocType Type
The source bloc type to subscribe to.
no setter
sourceEventType Type
The source event type this subscription listens for.
no setter
statusTypes Set<Type>
Which status types to listen for. Defaults to {UpdatingStatus} - only successful state updates.
final
toEvent → TLocalEvent Function(TSourceEvent sourceEvent)
Transforms the source event to a local event type.
final
useCaseGenerator UseCaseGenerator
Creates the use case instance that handles the local event.
final
when bool Function(TSourceEvent event)?
Optional predicate to filter which source events trigger execution.
final

Methods

close() Future<void>
Cleanup method called when the use case is disposed.
override
initialize() → void
Initializes the subscription. Called by JuiceBloc.
noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
setEventHandler(void handler(TLocalEvent event)) → void
Sets the callback for event dispatch. Called by JuiceBloc during registration.
toString() String
A string representation of this object.
inherited

Operators

operator ==(Object other) bool
The equality operator.
inherited