Created
July 8, 2019 16:41
-
-
Save myanmarlinks/f8e98e1869347da22df14293578f50b0 to your computer and use it in GitHub Desktop.
Dart Basic Part 4
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'; | |
main(List<String> arguments) { | |
var controller = StreamController<String>.broadcast(); | |
var streamTransformer = StreamTransformer<String, String>.fromHandlers( | |
handleData: (String data, EventSink sink) { | |
switch(data) { | |
case "fool": | |
sink.add("You $data"); | |
break; | |
case "idiot": | |
sink.add("You're $data"); | |
break; | |
default: | |
sink.add(data); | |
} | |
}, | |
handleError: (Object error, StackTrace stacktrace, EventSink sink) { | |
sink.addError('Something went wrong: $error'); | |
}, | |
handleDone: (EventSink sink) { | |
print("Controller Closed"); | |
sink.close(); | |
} | |
); | |
var streamController = controller.stream.transform(streamTransformer); | |
streamController.listen(print); | |
streamController.listen(print); | |
controller.sink.add("fool"); | |
controller.sink.add("idiot"); | |
controller.sink.add("Howdy"); | |
controller.addError("Hello"); | |
controller.sink.close(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment