Skip to content

Instantly share code, notes, and snippets.

@wackyapps
Created April 11, 2019 21:24
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/096041b35d0f10ff61e79d7f59abe51e to your computer and use it in GitHub Desktop.
Save wackyapps/096041b35d0f10ff61e79d7f59abe51e to your computer and use it in GitHub Desktop.
Broadcast Stream example in Dart
import 'dart:async';
void main() {
//
// Initialize a "Broadcast" Stream controller of integers
//
final StreamController<int> ctrl = StreamController<int>.broadcast();
//
// Initialize a single listener which filters out the off numbers and
// only prints the even numbers
final StreamSubscription subscription = ctrl.stream.where((value) => (value % 2 == 0)).listen((value) => print('Divisible by 2 : $value'));
final StreamSubscription subodd = ctrl.stream.where((value) => (value % 3 == 0)).listen((value) => print('Divisible by 3 : $value'));
//
// We here add the data that will flow inside the stream
//
for(int i = 1; i < 100; i++){
ctrl.sink.add(i);
}
//
// We release the StreamController
//
ctrl.close();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment