Skip to content

Instantly share code, notes, and snippets.

@wackyapps
Created April 11, 2019 21:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wackyapps/a7debe98a02d00d78cecfa7b71db80d4 to your computer and use it in GitHub Desktop.
Save wackyapps/a7debe98a02d00d78cecfa7b71db80d4 to your computer and use it in GitHub Desktop.
single subscription stream example in dart
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