subscribe abstract method

Subscription subscribe(
  1. Object eventType, {
  2. List<SubscriptionOpt>? opts,
})

Subscribe creates a new Subscription.

eventType can be either a single event type, or a list of types to subscribe to multiple event types at once, under a single subscription (and stream).

If you want to subscribe to ALL events emitted in the bus, use WildcardSubscription as the eventType:

eventbus.subscribe(WildcardSubscription)

Simple example:

var sub = await eventbus.subscribe(EventType); await sub.stream.forEach((event) { // The event is guaranteed to be of type EventType // Handle the event }); await sub.close();

Multi-type example:

var sub = await eventbus.subscribe(EventA, EventB); await sub.stream.forEach((event) { if (event is EventA) { // Handle EventA } else if (event is EventB) { // Handle EventB } }); await sub.close();

Implementation

Subscription subscribe(Object eventType , {List<SubscriptionOpt>? opts});