Skip to content

Instantly share code, notes, and snippets.

@ariefitriadin
Last active June 23, 2019 00:29
Show Gist options
  • Save ariefitriadin/8db16f38c345008a1d32fb878afe40d5 to your computer and use it in GitHub Desktop.
Save ariefitriadin/8db16f38c345008a1d32fb878afe40d5 to your computer and use it in GitHub Desktop.
Dart Stream Example
import 'dart:async';
class Cake {}
class Order {
String type;
Order(this.type);
}
void main() {
final controller = new StreamController();
final order = new Order('banana');
final baker = new StreamTransformer.fromHandlers(
handleData: (caketype, sink) {
if (caketype == 'chocolate') {
sink.add(new Cake());
} else {
sink.addError('I cant bake thats type !!');
}
}
);
controller.sink.add(order);
controller.stream
.map((order) => order.type)
.transform(baker)
.listen(
(cake) => print('heres your cake $cake'),
onError: (error) => print(error)
);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment