Created with <3 with dartpad.dev.
Created
July 18, 2022 08:11
-
-
Save mannuelf/4f12c2966c7d5b1a704f5a0c529b96e5 to your computer and use it in GitHub Desktop.
reactive-dart
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'; | |
void main() { | |
String chosenCake = 'chocolate'; | |
// Stream Controller | |
final controller = StreamController(); | |
final order = Order(chosenCake); | |
final baker = StreamTransformer.fromHandlers(handleData: (cakeType, sink) { | |
if (cakeType == 'chocolate') { | |
sink.add(Cake()); | |
} else { | |
sink.addError('Sorry I cannot bake that'); | |
} | |
}); | |
controller.sink.add(order); | |
controller.stream.map((order) => order.type).transform(baker).listen( | |
(cake) => print('here is the $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