Skip to content

Instantly share code, notes, and snippets.

@shinayser
Created August 9, 2019 20:34
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save shinayser/5cceb23d7ae575d30077d1fd108a4353 to your computer and use it in GitHub Desktop.
Save shinayser/5cceb23d7ae575d30077d1fd108a4353 to your computer and use it in GitHub Desktop.
A function that converts a ValueListenable to a Stream
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;
}
@01Vladimir10
Copy link

to prevent warnings for using the controller before it's assigned you should declare it with the late keyword:
late StreamController<T> controller;

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment