Observable provides a convenient mechanism of registering for events and dispatching notifications to listening clients.

How to use

Inheriting from Observable

The easiest way to use Observable is by inheriting from Observable, as given by:

import 'package:obvervable/obvervable.dart';

class Foo extends Observable

Clients register for notifications as illustrated in the following snippet:

Foo myFoo = new Foo();
myFoo.on.anEvent(() => print("AnEvent fired"));

Dispatching of events is done by invoking the dispatch method:

class Foo extends Observable
  void method()
    // carry out operations

Optionally, an argument can be passed in to dispatch:

observer.dispatch("anEvent", "anArg");

The above would cause "anArg" to be passed to be every event observer.


Since Dart does not support multiple inheritance, you may at times not be able to use the method outlined above. The alternative, then, is to use composition. Example:

import 'package:observable/observer.dart';
import 'package:observable/observer_events.dart';

class Foo extends SomeOtherClass
  Observer        observer;
  ObserverEvents  on;

    : observer = new Observer()
    on = observer.on;

Clients register for notifications and events are dispatched in the same manner as exemplified in the preceding section.



observable.dart --- Inheritable class.


observer.dart --- Class for easily notifying registered functions of events.


observer-events.dart --- Routes event register requests to an Observer instance.