Dispatch and listen events everywhere and whenever using Dart Stream. Also know as Event Bus
.
You can define a global event bus in your app, just like this:
import 'package:event_hub/event_hub.dart';
EventHub eventHub = EventHub();
eventHub.on('yourEventName', (dynamic data) {
// Your handler
print(data); // some optional data
});
You can cancel the subscription later if you want:
StreamSubscription subscription = eventHub.on('yourEventName', (_) {});
// ...
subscription.cancel();
eventHub.fire('yourEventName', 'some optional data');
That's all!
You can define your listeners or fire the events everywhere in your app. For instance, in a Flutter app, register the listeners in the initState
method in a Widget State and fire the event in another widget's tap handler.
The Event Hub
allows publishers to fire events and listeners to subscribe for events, everywhere in a Web or Flutter application.
For instance, in a Flutter application, we need below technology to communicate between components:
Works only when you want to notify ancestor widgets. You need to define the notification class, trigger a notification and create a notification listener.
More powerful, but it's heavy. You need to define the ChangeNotifier
class, when to notifier, notify whether or not, insert into the render tree, etc. Sometimes the elements will be rendered frequently when value changes, which is not expected, and will affect the performance.
You can pass callback as parameters, but this is dirty and not flexible. When there are many levels between the widgets you want to communicate, you have to pass the callback to each level.
This package is inspired by the EventBus
package: https://github.com/marcojakob/dart-event-bus, but with EventHub
, you even don't need to define a event class.
The MIT License (MIT)