on method

void Function() on(
  1. String event,
  2. void listener(
    1. AbilityUpdate
    )
)

Listens for rule changes. Returns a function that stops listening.

final off = ability.on('updated', (_) => setState(() {}));

Implementation

void Function() on(String event, void Function(AbilityUpdate) listener) {
  final listeners = switch (event) {
    'update' => _onUpdate,
    'updated' => _onUpdated,
    _ => throw ArgumentError.value(
      event,
      'event',
      'expected "update" or "updated"',
    ),
  };

  // A cascade would run the removal now rather than when the returned
  // function is called, which is the entire point of returning it.
  // ignore: cascade_invocations
  listeners.add(listener);

  return () => listeners.remove(listener);
}