Created
April 11, 2019 21:24
-
-
Save wackyapps/096041b35d0f10ff61e79d7f59abe51e to your computer and use it in GitHub Desktop.
Broadcast Stream example in Dart
This file contains hidden or 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 "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