Event<T> class
Generic event system for handling typed events with multiple listeners. This class provides a flexible event system that supports both synchronous and asynchronous listeners. Events can be emitted with typed data, and all registered listeners will be called in the order they were added. Features:
- Type-safe event data
- Support for both sync and async listeners
- Sequential execution of listeners
- Return count of executed listeners Example:
var userCreated = Event<User>();
userCreated.addListener((user) {
print('User created: ${user.name}');
});
userCreated.addAsyncListener((user) async {
await sendWelcomeEmail(user);
});
await userCreated.emit(newUser);
Constructors
- Event()
Properties
- hashCode → int
-
The hash code for this object.
no setterinherited
- runtimeType → Type
-
A representation of the runtime type of the object.
no setterinherited
Methods
-
addAsyncListener(
EventAsyncFunction listener) → Event - Adds an asynchronous listener to the event. Asynchronous listeners can perform async operations and are awaited during event emission. They are executed after all synchronous listeners have completed. Parameters:
-
addListener(
EventFunction listener) → Event - Adds a synchronous listener to the event. Synchronous listeners are called immediately and should complete quickly. They are executed before any asynchronous listeners. Parameters:
-
emit(
[dynamic data]) → Future< int> - Emits the event, calling all registered listeners. This method executes all synchronous listeners first, then all asynchronous listeners. Asynchronous listeners are awaited, so this method will not complete until all listeners have finished executing. Parameters:
-
noSuchMethod(
Invocation invocation) → dynamic -
Invoked when a nonexistent method or property is accessed.
inherited
-
toString(
) → String -
A string representation of this object.
inherited
Operators
-
operator ==(
Object other) → bool -
The equality operator.
inherited