如何将多个监听器分配给单个StreamSubscription? [英] How can you assign mutliple listeners to a single StreamSubscription?
问题描述
这是有效的语句
StreamSubscription currentSubscription = querySelector(…).onClick.listen(….).
这里有一个监听器分配给一个StreamSubscription。
here there is one listener that is assigned to one StreamSubscription.
但是当你做一个queryselectorAll(..)。onClick.listen(..)。
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(..)
在这里,我将一个1 StreamSubscription分配给带有onClick侦听器的元素列表。
Here, I am assigning a 1 StreamSubscription to a list of elements with onClick listeners. How does this work?
推荐答案
您不能将多个侦听器分配给 StreamSubscription
。
You can't assign multiple listeners to a StreamSubscription
.
您的第二个语句不起作用,因为 querySelectorAll()
返回一个集合,
Your 2nd statement doesn't work because querySelectorAll()
returns a collection and you can't listen to a collection.
也许这是你想要的:
Iterable<StreamSubscription> subscriptions =
queryselectorAll(..).map((e) => e.onClick.listen(..));
后来
if(subscriptions != null) {
subscriptions.forEach((s) => s.cancel());
}
这篇关于如何将多个监听器分配给单个StreamSubscription?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!