Skip to content

Instantly share code, notes, and snippets.

@Fallenstedt
Last active January 27, 2019 06:01
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 Fallenstedt/99b3677d0e5f6b6ed4123cdea4b0aa94 to your computer and use it in GitHub Desktop.
Save Fallenstedt/99b3677d0e5f6b6ed4123cdea4b0aa94 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 StreamController<Order> controller = new StreamController();
final Order cakeOrder = new Order('chocolate');
final Order cakeOrder2 = new Order('vanilla');
final StreamTransformer<String, Cake> baker = new StreamTransformer.fromHandlers(
handleData: (String cakeType, EventSink<Cake> sink) {
if (cakeType == 'chocolate') {
sink.add(new Cake());
} else {
sink.addError('I only bake chocoalte cakes. I cannot bake your cake type!');
}
}
);
controller.sink.add(cakeOrder);
controller.sink.add(cakeOrder2);
controller.stream
.map((Order o) => o.type)
.transform(baker)
.listen(
(cake) => print("Here is your cake! $cake"),
onError: (error) => print(error),
onDone: () => print("all done!")
);
controller.sink.close();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment