Created with <3 with dartpad.dev.
Created
August 17, 2022 07:51
-
-
Save Quingsley/f44f589eb692fa5111b32073b8002bba to your computer and use it in GitHub Desktop.
streams
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'; | |
void main() { | |
final controller = StreamController(); | |
final order = Order('chocolate'); | |
final baker = StreamTransformer.fromHandlers( | |
handleData:(cakeType,sink){ | |
if(cakeType == 'chocolate'){ | |
sink.add(Cake()); | |
}else { | |
sink.addError('I can\'t process this type'); | |
} | |
} | |
); | |
controller.sink.add(order); | |
controller.stream.map( | |
(order) => order.type | |
).transform(baker) | |
.listen( | |
(cake) => print('Here is your cake, $cake'), | |
onError:(err) => print(err) | |
); | |
} | |
class Cake{} | |
class Order{ | |
String type = ''; | |
Order(this.type); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment