eventnotifier 1.0.5

  • Readme
  • Changelog
  • Example
  • Installing
  • new70

EventNotifier #

Pub Package

Broadcasts named events to interested subscribers. When an event occurs, a method (callback) associated with the subscriber is executed.

EventNotifier is an implementation of the Observer Pattern, providing the ability to publish events, and subscribe to them elsewhere. Such implementations are sometimes called an 'Event Broker' or 'Event Bus'.

This is primarily intended to be mixed-in with a problem domain model, to enable it to provide change notifications.

Background #

It can be ideal to model problems or systems independent of user interfaces (UI) or system interfaces (SI). Doing so, one can model the problem or system without regard to how the model may (or may not be) consumed.

However, given an independent model, how can something else outside it know if something in the domain model changed? The answer is to use EventNotifier.

An Illustration #

Consider that one wanted to model the operation of a single 'Elevator'. One might produce an independent domain model in object-oriented Dart code that modeled it.

Later, one might want to provide a user interface (UI) that let one interact with the model, or provide a system interface (SI) that interacted with (for example) the Programmable Logic Controller (PLC) of a real-life Elevator.

In order to let external modules or systems know that the Elevator changed floors, the Elevator model can notify them through EventNotifier, so they can react as appropriate.

Flutter as User Interface (UI) #

If one wants to use Flutter as a user interface for an independent domain model, then there needs to be some way for Widgets to be notified that something has changed in the domain model. In our Elevator example model, the Elevator might be changing floors, but how would your Flutter Widget displaying the current floor number know that and update?

The answer is to subscribe to an event in the domain model using an EventSubscriber. This simple widget, allows one to subscribe to one or more named EventNotifier events. The Widget will be rebuilt when a subscribed event occurs, allowing some changing aspect of the model to be displayed in your Flutter user interface.

See also #

EventSubscriber - subscribe to named EventNotifier events in Flutter

Dependencies #

None. This Dart package has no non-development dependencies on any other packages.

Usage #

A simple example (see the static EventNotifier test method):

import 'package:eventnotifier/eventnotifier.dart';

main() {
    var e = EventNotifier();

    e.subscribe('myChange', (_) => print('boom'));
    e.subscribe('myChange', (args) => print(args['pi']));

    // number of event names
    assert(e.count() == 1);
    // number of handlers subscribed to the event name
    assert(e.count('myChange') == 2);

    // indicate that event 'myChange' has occured
    // rguments are only required if a subscriber
    // expects one or more
    e.notify('myChange', {'pi': 3.14159});

    // displays ...
    // boom
    // 3.14159
}

Features and bugs #

Please file feature requests and bugs at the issue tracker.

Changelog #

Version 1.0.5 (2020-01-14) #

  • Add support again for optional event arguments (was previously removed). Arguments should now be supplied as a Map, e.g. notify('myEvent', {'age': 32});
  • A more detailed error is shown if a subscriber expects an argument, but it isn't supplied when notified.
  • One can now access the EventNotifier using the getEventNotifier() method. Getting the instance can be useful from within a mixin.

Version 1.0.4 (2020-01-13) #

  • Fixed LICENSE reference to package

Version 1.0.3 (2020-01-13) #

  • Package example updated to show the static test() method.
  • Add a link to EventSubscriber in the README.

Version 1.0.2 (2020-01-12) #

  • Removed support for optional event arguments. Query the model instead.
  • Exceptions are now also output to the console.
  • README describes how to use Flutter with EventNotifier

Version 1.0.1 (2020-01-11) #

  • Improved README and minor reformatting changes

Version 1.0.0 (2020-01-11) #

  • Initial release

example/eventnotifier_example.dart

import 'package:eventnotifier/eventnotifier.dart';

// NOTE: EventNotifier is typically 'mixed-in' with a domain model

void main() {
  var e = EventNotifier();

  e.subscribe('myChange', (_) => print('boom'));
  e.subscribe('myChange', (args) => print(args['pi']));

  // number of event names
  assert(e.count() == 1);
  // number of handlers subscribed to the event name
  assert(e.count('myChange') == 2);

  // indicate that event 'myChange' has occured
  // rguments are only required if a subscriber
  // expects one or more
  e.notify('myChange', {'pi': 3.14159});

  // displays ...
  // boom
  // 3.14159
}

Use this package as a library

1. Depend on it

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


dependencies:
  eventnotifier: ^1.0.5

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:eventnotifier/eventnotifier.dart';
  
Popularity:
Describes how popular the package is relative to other packages. [more]
40
Health:
Code health derived from static analysis. [more]
99
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
100
Overall:
Weighted score of the above. [more]
70
Learn more about scoring.

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

  • Dart: 2.7.0
  • pana: 0.13.4

Health suggestions

Fix lib/src/eventnotifier.dart. (-1 points)

Analysis of lib/src/eventnotifier.dart reported 2 hints:

line 53 col 7: DO use curly braces for all flow control structures.

line 65 col 9: DO use curly braces for all flow control structures.

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.7.0 <3.0.0
Dev dependencies
pedantic ^1.8.0
test ^1.6.0