Jan Vladimir Mostert
Jan Vladimir Mostert

Reputation: 13002

Dart HTML: Clear onClick listeners on Button

Is there some kind of way to clear onClick listeners on a button in Dart HTML that doesn't require keeping track of all the listeners?

This code is executed each time a certain div is made visible and onClick listeners are attached to certain buttons, if it's hidden and shown again, more onClick listeners are attached and the old onClick event which is no longer valid is also being called.

querySelectorAll("button.update-something").forEach((ButtonElement b) {
    b.onClick.listen((e) {
        // Do Stuff
    });
}

Is there a way to clear existing onClick listeners or override the existing onClick listener without having to keep track of previous onClick listeners?

Upvotes: 3

Views: 2027

Answers (1)

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

Reputation: 657871

With keeping track you probably mean

List<StreamSubscriptions> subscriptions = [];

querySelectorAll("button.update-something").forEach((ButtonElement b) {
    subscriptions.add(b.onClick.listen((e) {
        // Do Stuff
    }));
}

I guess this is what you are looking for

querySelectorAll("button.update-something").forEach((ButtonElement b) {
    b.addEventListener('click', clickHandler);
}

void clickHandler(e) {
  // Do stuff
}

querySelectorAll("button.update-something").forEach((ButtonElement b) {
    b.removeEventListener('click', clickHandler);
}

Upvotes: 1

Related Questions