Created
April 11, 2019 21:16
-
-
Save wackyapps/a7debe98a02d00d78cecfa7b71db80d4 to your computer and use it in GitHub Desktop.
single subscription stream example in dart
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'; | |
void main() { | |
// | |
// Initialize a "Single-Subscription" Stream controller | |
// | |
final StreamController ctrl = StreamController(); | |
// | |
// Initialize a single listener which simply prints the data | |
// as soon as it receives it | |
// | |
final StreamSubscription subscription = ctrl.stream.listen((data) => print('$data')); | |
// | |
// We here add the data that will flow inside the stream | |
// | |
ctrl.sink.add('my name'); | |
ctrl.sink.add(1234); | |
ctrl.sink.add({'a': 'element A', 'b': 'element B'}); | |
ctrl.sink.add(123.45); | |
// | |
// We release the StreamController | |
// | |
ctrl.close(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment