Skip to content

Instantly share code, notes, and snippets.

@akmak1103
Last active June 12, 2020 09:54
Show Gist options
  • Save akmak1103/03aa2f8fc8eeaabed3fe829bd52147d4 to your computer and use it in GitHub Desktop.
Save akmak1103/03aa2f8fc8eeaabed3fe829bd52147d4 to your computer and use it in GitHub Desktop.
Creating a custom stream
import 'dart:async';
class DataCreator {
final _controller = StreamController<String>();
int _count = 1;
DataCreator() {
Timer.periodic(Duration(seconds: 1), (callBack) {
_controller.sink.add('String $_count');
_count++;
});
}
Stream<String> get stream => _controller.stream;
void dispose() {
_controller.close();
}
}
main() {
var subscription =
DataCreator().stream.listen((data) => print(data));
Timer(Duration(seconds:20),(){
subscription.cancel();
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment