In Dart, if I listen to a click event with two listeners, how do I know which happens first? In Dart, if I listen to a click event with two listeners, how do I know which happens first? dart dart

In Dart, if I listen to a click event with two listeners, how do I know which happens first?


Let's say I'm in other code that doesn't know anything about the first two click handlers, but I register another one.

Can I know that the stream has two listeners?

No, you can't. You could extend the stream class or wrap it and provide this functionality yourself, but it does not feel like a good design choice, because I don't think a listener should know about other listeners. What are you trying to do exactly? Perhaps there's a better way than letting listeners know about each other.

Can I pause or cancel all other subscribers?

You can cancel/pause/resume only the subscriber you are dealing with. Again, you probably shouldn't touch other listeners, but I guess you could wrap/extend the Stream class to have this behavior.

If I write button.onClick.asBroadcastStream() again elsewhere, does it point to the same stream as was used in main?

No, at least not at the current version of SDK. So, unfortunately, you need to store a reference to this broadcast stream somewhere, and refer to it, because calling asBroadcastStream() multiple times will not yield in the result you might expect. (Note: at least based on quick testing: http://d.pr/i/Ip0K although the documentation seems to indicate different, I have yet to test a bit more when I find the time).

Can I say in one of the handlers to not pass event on to the other broadcast listener?

Well, there's stopPropagation() in the HTML land which means that the event won't propagate to other elements, but it's probably not what you were looking for.

For being able to stop an event firing in other listeners, there needs to be an order of which the listeners are getting called. I believe the order is the order of registration of those listeners. From the design perspective, I don't think it would be a good idea to allow a listener to cancel/pause others.

Event propagation in HTML makes sense since it's about hierarchy, but here we don't have that (and even in case of events in HTML there can be multiple listeners for the single element).

There's no way to assign weight to listeners or define the order of importance, therefore it's not surprising that there isn't a way to stop the event.

Instead of letting listeners know about each other and manipulate each other, maybe you should try to think of another way to approach your problem (whatever that is).

Is that a consumer?

The StreamConsumer is just a class that you can implement if you want to allow other streams to be piped into your class.


Can I know that the stream has two listeners?

No, you have a ´Stream´ that wraps the DOM event handling. There is no such functionality.

Can I pause or cancel all other subscribers?

Look at Event.stopPropagation() and Event.stopImmediatePropagation(), and possibly Event.preventDefault().

If I write button.onClick.asBroadcastStream() again elsewhere, does it point to the same stream as was used in main?

[Updated] No, the current implementation doesn't gives you the same Stream back since the onClick getter returns a new stream every time it is invoked. However, the returned stream is already a broadcast stream so you shouldn't invoke asBroadcastStream() on it. If you do you will hower just get a reference to the same object back.

Stream<T> asBroadcastStream() => this;

Can I say in one of the handlers to not pass event on to the other broadcast listener? Is that a consumer?

Again, take a look at Event.stopPropagation() and Event.stopImmediatePropagation(), and possibly Event.preventDefault().