Skip to content

Instantly share code, notes, and snippets.

@ilikerobots
Last active June 20, 2016 12:58
Show Gist options
  • Save ilikerobots/70fd2ba01c1060d14240446211b53876 to your computer and use it in GitHub Desktop.
Save ilikerobots/70fd2ba01c1060d14240446211b53876 to your computer and use it in GitHub Desktop.
Basic examples of using Stream methods in Dart
import "dart:async";
main() async {
print(" * Starting *");
Function elPrinter = (e) { print("Element $e");};
List<int> numbers = new List<int>.generate(10, (i) => i); // 0 - 9
Stream<int> numberStream = new Stream.fromIterable(numbers);
numberStream.map((x) => x * 10) //0,10,20,30...
.expand((x) => [x, x+1,x+2,x+3 ]) //0,1,2,3,10,11,12,13...
.where( (x) => x.isEven) //0,2,10,12,20,22,30,32...
.takeWhile( (x) => x < 70) // ...50,52,50,62
.skip(2) // 10,12,20,22,30,32,40,42,50,52,60,62
.listen(elPrinter);
print(" * Ending * ");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment