event_hub 0.1.1

  • Readme
  • Changelog
  • Example
  • Installing
  • 78

Event Hub #

Star this Repo Pub Package

What #

Dispatch and listen events everywhere and whenever using Dart Stream. Also know as Event Bus.

How #

1. Create an Event Hub #

You can define a global event bus in your app, just like this:

import 'package:event_hub/event_hub.dart';

EventHub eventHub = EventHub();

2. Register Listeners #

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', (_) {});
// ...

3. Fire a event #

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.

Why #

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:

Notifications #

Works only when you want to notify ancestor widgets. You need to define the notification class, trigger a notification and create a notification listener.

ValueListenable / InheritedWidget / ChangeNotifierProvider #

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.

Callbacks #

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.

Event Bus #

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.

License #

The MIT License (MIT)

[0.1.1] - Add example

  • Created a simple Flutter example

[0.1.0] - Initial release

  • Core event hub feature: on & fire.


Event Hub Example #

Tap on the FloatingActionButton will fire event to increase the counter in the MyHomePage widget.

See https://pub.dev/packages/event_hub for more details.

Use this package as a library

1. Depend on it

Add this to your package's pubspec.yaml file:

  event_hub: ^0.1.1

2. Install it

You can install packages from the command line:

with pub:

$ pub get

with Flutter:

$ flutter pub get

Alternatively, your editor might support pub get or flutter pub get. Check the docs for your editor to learn more.

3. Import it

Now in your Dart code, you can use:

import 'package:event_hub/event_hub.dart';
Describes how popular the package is relative to other packages. [more]
Code health derived from static analysis. [more]
Reflects how tidy and up-to-date the package is. [more]
Weighted score of the above. [more]
Learn more about scoring.

We analyzed this package on Jan 14, 2020, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.7.0
  • pana: 0.13.4


Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.1.0 <3.0.0