Implement to create an event that can be used with the Mediator.
// Create a new event, must implement IEvent.
// The event only needs fields if the event subscribers will require them.
class NumberPickedEvent implements IEvent {
final int number;
NumberPickedEvent(this.number);
}
// OPTIONAL:
// I recommend creating an extension method for your events to make them
// a tiny bit nicer to run. This will hopefully be code-generated later on.
extension NumberPickedEventMediator on Mediator {
Future<void> raiseNumberPicked(int number) {
return raise(NumberPickedEvent(number));
}
}
Future<void> main() async {
// Create a new mediator, this should only be needed once per your application.
Mediator mediator = Mediator();
// Subscribe to the event, the callback will be called when the event is raised.
// You DO NOT have to unsubscribe, [WeakReference] will take care of it for you.
mediator.subscribe(
(NumberPickedEvent event) async =>
print("Number picked #1: ${event.number}"),
);
// Callbacks will be called in the order in which they are subscribed in.
// You can also manually unsubscribe if you wish to do it earlier for example.
EventSubscription subscription = mediator.subscribe(
(NumberPickedEvent event) async =>
print("Number picked #2: ${event.number}"),
);
await mediator.raise(NumberPickedEvent(123));
// Prints:
// Number picked #1: 123
// Number picked #2: 123
// If you implemented the optional extension method, then you can do this instead.
await mediator.raiseNumberPicked(123);
// In order to unsubscribe you just pass in the subscription returned by [subscribe].
mediator.unsubscribe(subscription);
}
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
-
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