Created
August 9, 2019 20:34
-
-
Save shinayser/5cceb23d7ae575d30077d1fd108a4353 to your computer and use it in GitHub Desktop.
A function that converts a ValueListenable to a Stream
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
Stream<T> valueListenableToStreamAdapter<T>(ValueListenable<T> listenable) { | |
StreamController<T> controller; | |
void listener() { | |
controller.add(listenable.value); | |
} | |
void start() { | |
listenable.addListener(listener); | |
} | |
void end() { | |
listenable.removeListener(listener); | |
} | |
controller = StreamController<T>( | |
onListen: start, | |
onPause: end, | |
onResume: start, | |
onCancel: end, | |
); | |
return controller.stream; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
to prevent warnings for using the controller before it's assigned you should declare it with the late keyword:
late StreamController<T> controller;