Last active
June 23, 2019 00:29
-
-
Save ariefitriadin/8db16f38c345008a1d32fb878afe40d5 to your computer and use it in GitHub Desktop.
Dart 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 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