What is the difference between listen and forEach in Dart streams?
The forEach
function on a Stream
will stop at the first error, and it won't give you a StreamSubscription
to control how you listen on a stream. Using forEach
is great if that's what you want - it tells you when it's done (the returned Future
) and all you have to do is handle the events. If you need more control, you can use the listen
call which is how forEach
is implemented.
It's like the difference between Iterable.forEach
and Iterable.iterator
- the former just calls a callback for each element, the other gives you a way to control the iteration.