StreamEventEmitter class

Event Stream Emitter

An Event-based system, highly inspired by NodeJS's Event Emitter. This implementation uses generic types to allow for multiple data types while being very intuitive.

Based on JavaScript and suitable for Dart and Flutter with type safety.

Features

  • Attach multiple listeners to an event emitter.
  • Listen to events with a specific event type and data type.
  • Emit an event of a specific type to be broadcasted to all listeners.
  • Type safety, only the right type will be passed in.
  • Use callbacks with EventEmitter.
  • Use streams with StreamEventEmitter.
  • Can be extended to create custom event emitters and events.
  • Custom events can hold custom data to be used in the listeners.

Usage:

StreamEventEmitter events = StreamEventEmitter();

events.on<String>('message').listen((String data) => print('String: $data'));
events.on<int>('message').listen((int data) => print('Integer: $data'));

events.emit('message', 'Hello World');
events.emit('message', 42);

// [Output]
// String: Hello World
// Integer: 42

Constructors

StreamEventEmitter({bool sync = true})

Properties

controller StreamController<Event>
final
hashCode int
The hash code for this object.
no setterinherited
runtimeType Type
A representation of the runtime type of the object.
no setterinherited

Methods

close() → void
Closes the event emitter, which closes all the listeners in the process.
emit<T>(String type, T data) → void
Emit a event with a specific event type and data type. This will broadcast the message to all listeners that match the same event type and data type.
emitEvent<T>(Event<T> event) → void
Emit a message on a specific event type and data type. This will broadcast the event to all listeners that match the same event type and data type.
noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
on<T>([String? type]) Stream<T>
Attach a listener to the event emitter. Calls the callback whenever there's a new event of the specified type and data type.
onAny<MessageType>() Stream<Event<MessageType>>
once<T>([String? type]) Future<T>
Same as on but only running once, by returning a Future.
onceEvent<T extends Event>([String? type]) Future<T>
Same as onEvent but only running once, by returning a Future.
onEvent<T extends Event>([String? type]) Stream<T>
Attach a listener to an emitter. Calls the callback whenever there's a new event of the specified type and data type.
toString() String
A string representation of this object.
inherited

Operators

operator ==(Object other) bool
The equality operator.
inherited