EventBus class

A type-safe event bus implementing the publish/subscribe pattern.

The EventBus allows decoupled communication between components using strongly-typed events.

Example

// Get the default instance
final bus = EventBus.instance;

// Subscribe to a specific event type
final unsubscribe = bus.on<UserCreatedEvent>((event) {
  print('User ${event.user.name} was created');
});

// Emit an event
bus.emit(UserCreatedEvent(user));

// Subscribe using streams
bus.stream<OrderPlacedEvent>().listen((event) {
  sendEmailConfirmation(event.order);
});

// Cleanup when done
unsubscribe();

Constructors

EventBus()
Creates a new event bus instance.

Properties

allEvents Stream<Event>
Stream of all events.
no setter
hashCode int
The hash code for this object.
no setterinherited
runtimeType Type
A representation of the runtime type of the object.
no setterinherited

Methods

clear() → void
Remove all listeners for all event types.
dispose() Future<void>
Dispose the event bus and release resources.
emit(Event event) → void
Emit an event to all subscribers.
noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
off<T extends Event>() → void
Remove all listeners for a specific event type.
on<T extends Event>(EventListener<T> listener) → void Function()
Subscribe to events of type T.
once<T extends Event>(EventListener<T> listener, {int count = 1}) → void
Subscribe to events of type T and automatically unsubscribe after receiving count events.
reset() → void
Reset the event bus (for testing).
stream<T extends Event>() Stream<T>
Returns a stream of events of type T.
toString() String
A string representation of this object.
inherited

Operators

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

Static Properties

instance EventBus
The global event bus instance.
final