Skip to main content

event_bus

PackageKindProvides the roleNeedsChoose with
smf_event_businfrastructureEventsnothing-m event_bus

event_bus lets parts of the app that do not know each other, such as two features, exchange events through one service. The service is built on the event_bus package.

What it adds to the app​

The module adds event_bus: ^2.0.1 to the dependencies, and lib/core/events/event_bus_communication_service.dart with the implementation of the service on an EventBus.

Whichever module provides it, the events role adds lib/core/events/communication_service.dart with:

  • AppEvent, the base class of the events, such as final class ItemAdded extends AppEvent;
  • the CommunicationService interface, where fire(event) sends an event and on<T>() is the stream of the events of type T;
  • createCommunicationService(), which returns the service of the app.

How events are delivered​

  • An event goes to everyone listening to its type, or to a type it extends or implements, so on<AppEvent>() gets every event.
  • Listeners get an event after the code that fires it has completed, not during fire.
  • A listener gets the events fired after it starts listening.

Getting the service​

The service is created on first use, without waiting, so the app starts as fast as it would without it. With a module of dependency injection, the events role registers the service in the container as a lazy singleton, and a feature takes it with resolve in its composition file. Without a container, the code of the app gets it from createCommunicationService(), which always returns the same service. Don't create a second one with createEventBusCommunicationService(), because the listeners of one service do not get the events fired into another. See Services and state.

Choosing it​

smf create asks which module provides the events of the app, and offers None as well. To choose it without the question:

smf create my_app -m event_bus