What is the use of Take method ? Flutter & dart

535 Views Asked by At

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);
}
1

There are 1 best solutions below

0
blackkara On BEST ANSWER

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);