Created
September 9, 2020 04:08
-
-
Save bhavikvashi1804/bb900d893830622d17c4c5a253cb619f to your computer and use it in GitHub Desktop.
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=new StreamController(); | |
final order1=new Order('Chocolate'); | |
//change the Order type to 'Banana Cake' | |
final baker=new StreamTransformer.fromHandlers( | |
handleData:(cakeType,sink){ | |
if(cakeType=='Chocolate'){ | |
sink.add(new Cake()); | |
}else{ | |
sink.addError('I can not bake that type'); | |
} | |
} | |
); | |
controller.sink.add(order1); | |
controller.stream | |
.map((order)=>order.type) | |
.transform(baker) | |
.listen( | |
(cake)=>print('Here is 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
This is Cake Factory: understanding of Streams in Dart