Stream Example
This file contains 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 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