Back to results for “Async await”
Flutter › Dart Streams & StreamControllers

What is a Stream in Dart, and how does it differ from a Future in terms of how many values it can actually provide over time?

Beginner

Answer

A Stream represents a sequence of asynchronous events or values delivered over time, potentially many separate values one after another, whereas a Future represents just a single value or error that will become available exactly once at some point in the future, and this makes a Stream well suited for things like listening to a continuous series of user text input changes, real time data updates from a server, or a sequence of individual button taps, where genuinely more than one value naturally needs to be delivered over the lifetime of that stream.
Stream<int> countStream() async* {
  for (int i = 1; i <= 5; i++) {
    yield i;
    await Future.delayed(Duration(seconds: 1));
  }
}
Real-world example A live sports score app uses a Stream to continuously receive a whole sequence of updated score values throughout an ongoing match, rather than a Future which could only ever deliver one single final result.

Common follow-ups: What is the difference between a single subscription stream and a broadcast stream?;How do you properly listen to a Stream from within a Flutter widget?