Skip to content
tezvyn:

Dart Streams: Asynchronous Data Sequences

Source: dart.devHardHow cards are made

A Dart Stream is like a conveyor belt for asynchronous data, delivering events or file chunks as they arrive. Use them for continuous data flows like button clicks or reading large files.

Why it exists

While a Future represents a single value that will be available later, many asynchronous operations involve a sequence of values over time. Dart needed a way to represent these asynchronous sequences, like ongoing user input or data arriving in chunks over a network.

The mental model

A Future is like waiting for a single package to be delivered. A Stream is the entire conveyor belt, bringing a series of packages over time. You subscribe to the stream to process each package as it arrives, and you can also handle errors if a package is damaged or the belt breaks.

How it works

You consume a stream in two primary ways. The first is with an await for loop, which lets you process each event in a way that looks like a synchronous for loop. The second is by calling the .listen() method on the stream directly. This provides callbacks for handling data events (onData), errors (onError), and the stream's completion (onDone). Streams can also be transformed, creating a new stream from an old one, for example, by filtering or mapping events.

When to use it

Use streams for any sequence of asynchronous events. This is common in Flutter for listening to UI events like button presses, tracking authentication state changes from a service like Firebase, or receiving data from a WebSocket. They are also ideal for reading large files from disk without loading the entire file into memory at once.

When not to use it

If you only need a single asynchronous value, a Future is simpler and more appropriate. Using a stream for a one-off network request that returns a single JSON object is overkill; a Future is the correct tool for that job.

One canonical example

Streams come in two flavors. A single-subscription stream is for a sequence that is consumed in its entirety by one listener, like reading a file. It will buffer data until a listener subscribes. A broadcast stream is for events that can have multiple listeners, like a stream of button clicks. It fires events as they happen, and new listeners won't receive past events. A common footgun is trying to listen to a single-subscription stream more than once, which will throw a runtime error.

Interview question

When is a Dart Stream the most appropriate choice for handling asynchronous data?

  • a.Waiting for the result of a complex, time-consuming mathematical computation.
  • b.Loading a configuration file that contains multiple settings at application startup.
  • c.Retrieving a user's complete shopping cart details from a database.
  • d.Processing a series of real-time sensor readings from an IoT device.Correct
Why?

Option D describes a continuous flow of data (real-time sensor readings), which is the primary use case for Dart Streams, as they handle sequences of asynchronous events over time. Options A, C, and D all represent scenarios where a single asynchronous value is expected, making a Future a more appropriate and simpler choice.

Just read this? Test yourself on what you have been reading.

Read the original → dart.dev

Put your scrolling time to good use

Learn one idea, try a quiz and save useful cards for revision. Tezvyn makes it easy to learn and stay current in your tech field, a few minutes at a time.

The iPhone app is on the way

We are building it. Until it lands, nothing here is held back from you: every interview card, your saved cards, streaks and the job board all work in Safari, plus hundreds of free practice quizzes of thirty questions each. Sign in and it all carries over to the app the day it arrives.

Want it as an icon? Tap Share at the bottom of Safari, then Add to Home Screen. It opens full screen and the cards you have read stay available offline.

Get it on Google PlayiPhone app coming soon

We are hiring for this. Open roles that interview on dart — each one lists the topics its interview covers.

See open roles