Making Custom Events with Streams

Dart includes dozens of event streams in the dart:html library alone. In addition to the MouseEvent stream that we have already explored, Dart has streams for everything from Ajax events to application cache events. But sometimes that is not enough.

As mentioned in the previous section, Dart streams do not expose a way to add events or data. It might seem as though streams are useless should we want a stream on which other code can listen for our custom events. Well, streams are far from useless. In this case we need a StreamController.

Revisiting the ComicsCollection class, we have already briefly seen custom “on” properties like onLoad and onAdd.

events/collection_events.dart
 
class​ ComicsCollection ​extends ...

Get Dart 1 for Everyone now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.