Listen to events on ElementList with no explicit accessor

Issue

Is it possible to listen to events that have no explicit accessor on an ElementList?

For example I can do the following on a single element:

this.querySelector(".js-popover-link").on["on-tap"].listen((event) {
  print("Event Triggered");
});

However, the following is not possible on the ElementList returned from querySelectorAll:

this.querySelectorAll(".js-popover-link").on["on-tap"].listen((event) {
  print("Event Triggered");
});

Is there a simple way to do this?

Solution

List<StreamSubscription> _subscriptions = <StreamSubscription>[];

this.querySelectorAll(".js-popover-link")
.forEach((e) {
  _subscriptions.add(e.on["on-tap"].listen((event) {
    print("Event Triggered");
  }));
});

_subscriptions.forEach((s) => s.cancel());

Answered By – Günter Zöchbauer

Answer Checked By – David Marino (FlutterFixes Volunteer)

Leave a Reply

Your email address will not be published. Required fields are marked *