smf_event_bus 0.3.0
smf_event_bus: ^0.3.0 copied to clipboard
The SMF module of events with event_bus for the Flutter app that SMF generates. Parts of the app that do not know each other exchange events through it.
Generate a Flutter app with an event bus #
event_bus is the module of the SMF CLI through which parts of the app that do not know each other exchange events. Choose it with -m:
dart pub global activate smf_flutter_cli
smf create my_app -m event_bus --no-input
The module adds event_bus to the pubspec.yaml of the app, and lib/core/events/event_bus_communication_service.dart with the service on an EventBus. The rest of the app uses the interface in lib/core/events/communication_service.dart, which the events role adds:
/// Delivers [AppEvent]s between parts of the app that do not know each
/// other.
abstract interface class CommunicationService {
/// Sends [event] to everyone listening to its type.
void fire(AppEvent event);
/// The events of type [T] sent from now on.
Stream<T> on<T extends AppEvent>();
}
An event is a subclass of AppEvent. One part of the app listens, another fires:
final class ItemAdded extends AppEvent {
const ItemAdded();
}
final events = createCommunicationService();
events.on<ItemAdded>().listen((event) => debugPrint('An item was added'));
events.fire(const ItemAdded());
With a module of dependency injection, such as get_it, the service is registered in the container, and a feature takes it with resolve in its composition file.
Without -m, smf create asks which module provides the events of the app.
The documentation has more on the event_bus module and on events in the generated app.