Event Bus
The EventBus is the primary way for modules to communicate without being tightly coupled. It supports both strongly-typed events and lightweight named signals.
Typed Events
Section titled “Typed Events”Use Typed Events for critical business logic where you need compile-time safety.
1. Define an Event
Section titled “1. Define an Event”Extend ModuleEvent and provide a sourceModuleId.
class UserLoggedInEvent extends ModuleEvent { final String userId; final String email;
UserLoggedInEvent({ required super.sourceModuleId, required this.userId, required this.email, });}2. Emit and Listen
Section titled “2. Emit and Listen”// EmitEventBus().emit(UserLoggedInEvent( sourceModuleId: 'auth', userId: '123', email: 'user@example.com',));
// ListenEventBus().on<UserLoggedInEvent>((event) { print('User ${event.email} logged in');}, subscriberModuleId: 'home');Named Signals
Section titled “Named Signals”Signals are lightweight, dynamic messages identified by a string name.
// EmitEventBus().emitSignal('cart.updated', data: {'count': 5}, sourceModuleId: 'cart');
// ListenEventBus().onSignal('cart.updated', (data) { print('New count: ${data['count']}');}, subscriberModuleId: 'header');Middleware
Section titled “Middleware”You can intercept all events or signals to add global logic like logging or analytics.
EventBus().addMiddleware((event, next) { print('Log: ${event.runtimeType} from ${event.sourceModuleId}'); next(event); // Continue the chain});Schema Validation (Optional)
Section titled “Schema Validation (Optional)”For large teams, you can enforce the structure of signals at runtime.
EventSchemaValidator().registerSignalSchema( 'user.register', MapSchema({ 'email': StringSchema(required: true), 'age': NumberSchema(min: 18), }),);