Eternalcode
Eternalcode

Reputation: 2412

How can you assign mutliple listeners to a single StreamSubscription?

This is a valid statement

StreamSubscription currentSubscription = querySelector(…).onClick.listen(….).

here there is one listener that is assigned to one StreamSubscription.

But when you do a queryselectorAll(..).onClick.listen(..) . You are adding listeners to all the query selected elements.

I noticed that the following statement is also valid.

StreamSubscription currentSubscription = queryselectorAll(..).onClick.listen(..) 

Here, I am assigning a 1 StreamSubscription to a list of elements with onClick listeners. How does this work?

Upvotes: 1

Views: 565

Answers (2)

lrn
lrn

Reputation: 71853

You are listening on the ElementList. I can't see from the documentation how that is expected to work, so I guess it is effectively receiving the events of all the elements in the list.

You are still only listening on one stream, the onClick stream of the ElementList, and the StreamSubscription you receive relates to that list.

Upvotes: 1

Günter Zöchbauer
Günter Zöchbauer

Reputation: 658135

You can't assign multiple listeners to a StreamSubscription.

Your 2nd statement doesn't work because querySelectorAll() returns a collection and you can't listen to a collection.

Maybe this is what you want:

Iterable<StreamSubscription> subscriptions = 
    queryselectorAll(..).map((e) => e.onClick.listen(..));

and then later

if(subscriptions != null) {
  subscriptions.forEach((s) => s.cancel());
}

Upvotes: 3

Related Questions