Search code examples
flutterdarttake

What is the use of Take method ? Flutter & dart


take-->What role does it play (flutter and Dart)

class Ticker {
  Stream<int> tick({int ticks}) => Stream.periodic(Duration(seconds: 1), (x) {
        return ticks - x - 1;
      }).take(ticks);
}

Solution

  • So simply, it just takes items as count of you pass(ticks parameter) from a stream.

    // This stream is responsible to do a task for each second 
    Stream.periodic(Duration(seconds: 1), (x) {
        return ticks - x - 1;
    })
    
    // With take, stream does it's job only `ticks` times
    Stream.periodic(Duration(seconds: 1), (x) {
        return ticks - x - 1;
    }).take(ticks);