event_bus 0.4.1

  • README.md
  • Example
  • Installing
  • Versions
  • 74

Event Bus

A simple Event Bus using Dart Streams for decoupling applications.

Star this Repo Pub Package Build Status

GitHub | Pub | Demos and Examples

Event Bus Pattern

An Event Bus follows the publish/subscribe pattern. It allows listeners to subscribe for events and publishers to fire events. This enables objects to interact without requiring to explicitly define listeners and keeping track of them.

Event Bus and MVC

The Event Bus pattern is especially helpful for decoupling MVC (or MVP) applications.

One group of MVC is not a problem.


But as soon as there are multiple groups of MVCs, those groups will have to talk to each other. This creates a tight coupling between the controllers.

Multi Model-View-Controllers

By communication through an Event Bus, the coupling is reduced.

Event Bus


1. Create an Event Bus

Create an instance of EventBus and make it available to other classes.

Usually there is just one Event Bus per application, but more than one may be set up to group a specific set of events.

import 'package:event_bus/event_bus.dart';

EventBus eventBus = new EventBus();

You can alternatively use the HierarchicalEventBus that filters events by event class including its subclasses.

Note that the hierarchical event bus uses dart:mirrors which support in dart2js is experimental.

import 'package:event_bus/event_bus_hierarchical.dart';

EventBus eventBus = new HierarchicalEventBus();

Note: The default constructor will create an asynchronous event bus. To create a synchronous you must provide the optional sync: true attribute.

2. Define Events

Any Dart class can be used as an event.

class UserLoggedInEvent {
  User user;

class NewOrderEvent {
  Order order;

3. Register Listeners

Register listeners for a specific events:

eventBus.on(UserLoggedInEvent).listen((UserLoggedInEvent event) {

Register listeners for all events:

eventBus.on().listen((event) {
  // Print the runtime type. Such a set up could be used for logging.

About Dart Streams

EventBus uses Dart Streams as its underlying mechanism to keep track of listeners. You may use all functionality available by the Stream API. One example is the use of StreamSubscriptions to later unsubscribe from the events.

StreamSubscription loginSubscription = eventBus.on(UserLoggedInEvent).listen((UserLoggedInEvent event) {


4. Fire Events

Finally, we need to fire an event.

User myUser = new User('Mickey');
eventBus.fire(new UserLoggedInEvent(myUser));


The MIT License (MIT)


Version 0.4.1 (2015-05-13)

  • Fix Issue #13: Improve on() stream when no type is specified

Version 0.4.0 (2015-05-03)

  • BREAKING CHANGE: Moved the HierarchicalEventBus to a separate library to be able to remove dart:mirrors from the normal EventBus.
    Users of the hierarchical event bus must import event_bus_hierarchical.dart and replace the use of the factory constructor EventBus.hierarchical() with the HierarchicalEventBus constructor.

Version 0.3.0 (2014-09-08)

  • BREAKING CHANGE: Changed and simplified the EventBus API. We can now dispatch any Dart object as an event. Before, we had to create an EventType for every type of event we wanted to fire. Now we can use any class as an event. Listeners can (optionally) filter events by that class.
  • Added a way to create a hierarchical event bus that filters events by class and their subclasses. This currently only works with classes extending other classes and not with implementing an interface. We might have to wait for https://code.google.com/p/dart/issues/detail?id=20756 to enable interfaces.
  • BREAKING CHANGE: The EventBus constructor defaults to async instead of sync!!. This matches the constructor of the Dart Streams and an async event bus might also be the more common use case.
  • BREAKING CHANGE: Removed LoggingEventBus. Reason is that logging can easily be implemented with a event listener that listens for all events and logs them.

Version 0.2.5 (2014-09-03)

  • Update example.
  • Update readme with new links.
  • Update dependencies.

Version 0.2.4 (2013-11-11)

  • Update to dart libraries 0.9.0.

Version 0.2.3 (2013-09-16)

  • Fix issue #8: Add logging of events that flow through event bus

Version 0.2.2 (2013-09-16)

  • Change default of SimpleEventBus to sync (same as factory in EventBus)

Version 0.2.1 (2013-07-01)

  • Fix issue #6: Fire should accept null as data

Version 0.2.0 (2013-06-06)

  • Update to new Dart SDK v0.5.13.1_r23552.
  • Using Darts new Stream.broadcast() factory.
  • Provide option for synchronous broadcasting of events.
  • Update unit tests and example.
  • Create demo page.

Version 0.1.3 (2013-05-19)

  • Removed all occurrences of @override

Version 0.1.2 (2013-05-17)

  • Change in README: contained wrong license (Apache instead of MIT).
  • Remove import 'package:meta/meta.dart' in event_bus.dart as it is not needed and may cause an error if used as pub package.

Version 0.1.1 (2013-04-29)

  • Minor change in README to fix image links.

Version 0.1.0 (2013-04-29)

  • Initial Version.


import 'dart:html';

import 'dart:async';
import 'events.dart';
import 'package:logging/logging.dart';

int counterA = 1;
int counterB = 1;

final _log = new Logger('event_bus_example');

void main() {
  // Init logging.
  // Log all events.
  eventBus.on().listen((event) => _log.finest('event fired:  ${event.runtimeType}'));
  // Initialize the listener boxes.
  Listener listener1 = new Listener(querySelector('#listener-1'));
  Listener listener2 = new Listener(querySelector('#listener-2'));
  // Init Event fields.
  LabelElement fireLabelA = querySelector('#fire-label-a');
  LabelElement fireLabelB = querySelector('#fire-label-b');
  ButtonElement fireButtonA = querySelector("#fire-button-a");
  ButtonElement fireButtonB = querySelector("#fire-button-b");
  fireButtonA.onClick.listen((_) {
    // -------------------------------------------------
    // Fire Event A
    // -------------------------------------------------
    eventBus.fire(new MyEventA('Received Event A [$counterA]'));
    fireLabelA.text = '--> fired [$counterA]';
  fireButtonB.onClick.listen((_) {
    // -------------------------------------------------
    // Fire Event B
    // -------------------------------------------------
    eventBus.fire(new MyEventB('Received Event B [$counterB]'));
    fireLabelB.text = '--> fired [$counterB]';

initLogging() {
  // Print output to console.
  Logger.root.onRecord.listen((LogRecord r) {
  // Root logger level.
  Logger.root.level = Level.FINEST;

class Listener {
  Element element;
  TextAreaElement output;
  StreamSubscription<String> subscription;
  Listener(this.element) {
    output = element.querySelector('textarea');
    // Init buttons.
    element.querySelector('.listen-a').onClick.listen((_) => listenForEventA());
    element.querySelector('.listen-b').onClick.listen((_) => listenForEventB());
    element.querySelector('.pause').onClick.listen((_) => pause());
    element.querySelector('.resume').onClick.listen((_) => resume());
    element.querySelector('.cancel').onClick.listen((_) => cancel());
  void listenForEventA() {
    if (subscription != null) {
      appendOuput('Already listening for an event.');
    } else {
      // -------------------------------------------------
      // Listen for Event A
      // -------------------------------------------------
      subscription = eventBus.on(MyEventA).listen((event) {
      appendOuput('Listening for Event A');
  void listenForEventB() {
    if (subscription != null) {
      appendOuput('Already listening for an event.');
    } else {
      // -------------------------------------------------
      // Listen for Event B
      // -------------------------------------------------
      subscription = eventBus.on(MyEventB).listen((MyEventB event) {
      appendOuput('Listening for Event B');
  void pause() {
    if (subscription != null) {
      appendOuput('Subscription paused.');
    } else {
      appendOuput('No subscription, cannot pause!');
  void resume() {
    if (subscription != null) {
      appendOuput('Subscription resumed.');
    } else {
      appendOuput('No subscription, cannot resume!');
  void cancel() {
    if (subscription != null) {
      subscription = null;
      appendOuput('Subscription canceled.');
    } else {
      appendOuput('No subscription, cannot cancel!');
  void appendOuput(String text) {
    output.scrollTop = output.scrollHeight;

Use this package as a library

1. Depend on it

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

  event_bus: "^0.4.1"

2. Install it

You can install packages from the command line:

with pub:

$ pub get

with Flutter:

$ flutter packages get

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

3. Import it

Now in your Dart code, you can use:

      import 'package:event_bus/event_bus.dart';
Version Uploaded Documentation Archive
0.4.1 May 13, 2015 Go to the documentation of event_bus 0.4.1 Download event_bus 0.4.1 archive
0.4.0 May 3, 2015 Go to the documentation of event_bus 0.4.0 Download event_bus 0.4.0 archive
0.3.0+3 Mar 9, 2015 Go to the documentation of event_bus 0.3.0+3 Download event_bus 0.3.0+3 archive
0.3.0+2 Sep 8, 2014 Go to the documentation of event_bus 0.3.0+2 Download event_bus 0.3.0+2 archive
0.3.0+1 Sep 8, 2014 Go to the documentation of event_bus 0.3.0+1 Download event_bus 0.3.0+1 archive
0.3.0 Sep 8, 2014 Go to the documentation of event_bus 0.3.0 Download event_bus 0.3.0 archive
0.2.4 Nov 11, 2013 Go to the documentation of event_bus 0.2.4 Download event_bus 0.2.4 archive
0.2.3 Sep 16, 2013 Go to the documentation of event_bus 0.2.3 Download event_bus 0.2.3 archive
0.2.2 Sep 16, 2013 Go to the documentation of event_bus 0.2.2 Download event_bus 0.2.2 archive
0.2.1 Jul 1, 2013 Go to the documentation of event_bus 0.2.1 Download event_bus 0.2.1 archive

All 15 versions...


We analyzed this package on Jun 19, 2018, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.0.0-dev.63.0
  • pana: 0.11.3


Describes how popular the package is relative to other packages. [more]
90 / 100
Code health derived from static analysis. [more]
96 / 100
Reflects how tidy and up-to-date the package is. [more]
0 / 100
Overall score:
Weighted score of the above. [more]
Learn more about scoring.


Detected platforms: Flutter, web, other

No platform restriction found in primary library package:event_bus/event_bus.dart.


  • Documentation URL is not helpful.

    Update the documentation property: create a website about the package or remove it.

  • Package is too old.

    The package was released more than two years ago.

  • Package is pre-v1 release.

    While there is nothing inherently wrong with versions of 0.*.*, it usually means that the author is still experimenting with the general direction API.

  • Fix analysis and formatting issues.

    Analysis or formatting checks reported 3 hints.

    Run dartfmt to format lib/event_bus.dart.

    Run dartfmt to format lib/event_bus_hierarchical.dart.


Package Constraint Resolved Available
Dev dependencies
browser >=0.10.0 <0.11.0
logging >=0.10.0 <0.11.0
unittest >=0.11.0 <0.12.0