EventBus class
EventBus - A publish-subscribe messaging system for decoupled communication
The EventBus allows different parts of your application to communicate without direct dependencies. Components can publish events and other components can subscribe to those events.
All events are string-based, eliminating the need for shared type definitions between plugins. This enables true plugin independence.
Basic Example:
// Subscribe to an event
final subscription = EventBus().on('user.logged.in', (event) {
print('User logged in: ${event.data['userId']}');
});
// Fire an event
EventBus().fire('user.logged.in', data: {'userId': 'user123'});
// Unsubscribe when done
await subscription.cancel();
Event Naming Convention:
Use dot notation: <domain>.<action>.<optional-detail>
cart.item.addeduser.profile.updatedpayment.completednotification.sent
Plugin Communication Example:
// Payment plugin fires event (no dependency on other plugins)
EventBus().fire('payment.completed', data: {
'orderId': 'order-123',
'amount': 99.99,
});
// Analytics plugin listens (no dependency on payment plugin!)
EventBus().on('payment.completed', (event) {
trackPayment(event.data['orderId'], event.data['amount']);
});
Constructors
- EventBus()
-
Get the singleton instance
factory
Properties
- activeSubscriptionCount → int
-
Get the number of active subscriptions
no setter
- hashCode → int
-
The hash code for this object.
no setterinherited
- registeredEventCount → int
-
Get the number of registered event names
no setter
- runtimeType → Type
-
A representation of the runtime type of the object.
no setterinherited
Methods
-
cancelAllSubscriptions(
) → Future< void> - Cancel all active subscriptions
-
cancelSubscriptionsForEvent(
String eventName) → Future< void> - Cancel all active subscriptions for a specific event name
-
destroy(
) → Future< void> - Destroy the event bus and clean up all resources
-
fire(
String eventName, {Map< String, dynamic> ? data, Map<String, dynamic> ? metadata}) → void - Fire an event by name
-
fireAndWait(
String eventName, {Map< String, dynamic> ? data, Map<String, dynamic> ? metadata}) → Future<void> - Fire an event and wait for all handlers to complete
-
getRegisteredEvents(
) → List< String> - Get all registered event names
-
hasSubscribers(
String eventName) → bool - Check if there are any subscribers for a specific event
-
noSuchMethod(
Invocation invocation) → dynamic -
Invoked when a nonexistent method or property is accessed.
inherited
-
on(
String eventName, void onEvent(Event event), {Function? onError, void onDone()?}) → EventSubscription - Subscribe to events by name
-
onAsync(
String eventName, Future< void> onEvent(Event event), {Function? onError, void onDone()?}) → EventSubscription - Subscribe to events and handle them asynchronously
-
reset(
) → Future< void> - Reset the event bus (useful for testing)
-
stream(
String eventName) → Stream< Event> - Get a stream of events by event name
-
toString(
) → String -
A string representation of this object.
inherited
Operators
-
operator ==(
Object other) → bool -
The equality operator.
inherited