Skip to content

Instantly share code, notes, and snippets.

@DrBeta
Last active May 15, 2019 23:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save DrBeta/0b65db8fd70e9e4fcf9695d856a803a1 to your computer and use it in GitHub Desktop.
Save DrBeta/0b65db8fd70e9e4fcf9695d856a803a1 to your computer and use it in GitHub Desktop.
Stream Example
import 'dart:async';
class Cake {}
class Order {
String type;
Order(this.type);
}
void main() {
final controller = StreamController();
final order = new Order('chocolate');
final baker = new StreamTransformer.fromHandlers(
handleData: (cakeType, sink) {
if (cakeType == 'chocolate') {
sink.add(new Cake());
} else {
sink.addError('I cant bake that type');
}
}
);
controller.sink.add(order);
controller.stream.map((order) => order.type).transform(baker).listen(
(cake) => print('Heres your cake $cake'),
onError: (err) => print(err));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment