Reputation: 553
I have a class that creates a button and connects a listener to the click event. In a sub class I'd like to replace the superclass handler. This code adds a listener:
row.query("[value='Save']").onClick.listen(handleNewAlert);
How do I remove the existing listener?
Upvotes: 2
Views: 970
Reputation: 16261
The Stream.listen() method returns a StreamSubscription object. Call StreamSubscription.cancel() to cancel the event listener.
var subs = element.onClick.listen((e) => print(e));
// Remove the listener.
subs.cancel();
// Add another listener.
element.onClick.listen((e) => print(e));
See this article for more information.
Upvotes: 6
Reputation: 14581
Greg's answer describes how to unsubscribe from the event source. This is correct, but IMO a bit awkward, as you need to keep the subscription instance around in case that any derived class wants to cancel it, or you need to provide a method for this on the base class if you want to hide the details.
As you say that you want to replace the handler, which I understand as providing a different implementation, a simpler approach is to simlpy override handleNewAlert
method in the derived class.
Of course, it won't work if instead of method name you have specified an anonymous function, as it obviously can't be overridden, but in the scenario shown in your example, this might be an easier approach.
Upvotes: 2