event 3.1.0 copy "event: ^3.1.0" to clipboard
event: ^3.1.0 copied to clipboard

Create lightweight custom Events, that allow interested subscribers to be notified that something has happened.

example/event_example.dart

import 'dart:io';

import 'package:event/event.dart';

/*
Let's define a domain model independent of any user interface.
There is only a single dependency - the Event package - which
lets one...

(#1) declare an Event (something happened),
(#2) subscribe some code to the Event,
(#3) and broadcast to subscribers that the event occurred,
    and the subscribed code should run.

One could have multiple clients use the domain model, be
notified when things change, and then query the domain model
for what has changed.

Note: not shown here is that Events can pass data to
clients, although the more typical architectural model is that
the client will query the model to determine the model state.

 */

/// Represents an example number counter that can be incremented.
class Counter {
  /// The current [Counter] value.
  int count = 0;
  final countChangedEvent = Event("countChanged"); // (#1)

  /// Increment the [Counter] [count] by 1.
  void increment() {
    count++;
    countChangedEvent.broadcast(); // (#3)
  }

  /// Reset the [Counter] [count] to 0.
  void reset() {
    count = 0;
    countChangedEvent.broadcast(); // (#3)
  }
}

//-----------------
// Use the Counter class and be notified when it changes.

void main() {
  // enable log output
  showLog(stdout, Severity.info.value);

  // Note: a debug level log message occurs when an event is broadcast
  // Try changing showLog to ...
  // showLog(stdout, Severity.info.value | Severity.debug.value);
  // or
  // showLog(stdout, Severity.all);

  var c = Counter();

  // Subscribe code to run when the Event occurs. (#2)
  c.countChangedEvent.subscribe((args) {
    log('count changed to ${c.count} at ${args.whenOccurred}',
        source: "counter", level: Severity.info);
  });

  // Increment the Counter twice. Subscribers are notified,
  // resulting in the print statement above being executed
  // twice.
  c.increment();
  c.increment();

  // Reset the Counter to 0. Subscribers are notified,
  // resulting in the print statement above being executed.
  c.reset();
}
48
likes
150
points
10.1k
downloads

Publisher

verified publisheraryehoffman.com

Weekly Downloads

Create lightweight custom Events, that allow interested subscribers to be notified that something has happened.

Repository (GitHub)
View/report issues

Documentation

API reference

License

Apache-2.0 (license)

More

Packages that depend on event