Last active
June 12, 2020 09:54
-
-
Save akmak1103/03aa2f8fc8eeaabed3fe829bd52147d4 to your computer and use it in GitHub Desktop.
Creating a custom 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
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