draw-dev / DRAW

DRAW: The Dart Reddit API Wrapper
BSD 3-Clause "New" or "Revised" License
87 stars 24 forks source link

Why do many of these endpoints return Streams instead of Lists? #175

Closed ThinkDigitalSoftware closed 4 years ago

ThinkDigitalSoftware commented 4 years ago

I'm used to getting a list of say, posts, rather than a stream that returns a single value at a time. Is there a reason for this?

bkonyi commented 4 years ago

Streams allow for data to be automatically fetched asynchronously when the backing data store is exhausted (e.g., a user has iterated over 100 comments, the next 100 comments need to be fetched from the network). You can get a list from a Dart stream by calling stream.toList() or you can simply loop over the stream to build a list yourself:

final list = [];
await for (final value in stream) {
  list.add(value);
}