Last active
January 27, 2019 06:01
-
-
Save Fallenstedt/99b3677d0e5f6b6ed4123cdea4b0aa94 to your computer and use it in GitHub Desktop.
Stream example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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