Kotlin Flow: Asynchronous Data Streams

A Kotlin Flow is like an async Sequence, emitting multiple values over time without blocking. It's used for live data from a database or streaming network responses. The footgun: Flows are cold; the code doesn't run until a collector calls .collect().
Why it exists
A suspending function can return a single value asynchronously, like a complete list of items. But what if you need to receive those items one by one as they become available, without blocking the thread? This is the problem Flow was designed to solve: representing a stream of multiple, asynchronously computed values.
The mental model
Think of a Flow as an asynchronous Sequence. A Sequence produces values one-by-one, but it's synchronous and blocks the thread while computing the next value. A Flow "emits" values one-by-one, but it can suspend between emissions, freeing up the thread for other work. It's a stream of data you can observe over time.
How it works
You create a stream of data using the flow { ... } builder. Inside this block, you can call suspending functions (like delay or network requests) and send values to the consumer using the emit() function. The function that returns the Flow is not marked suspend. To start receiving values, a consumer coroutine calls a terminal operator like .collect { value -> ... }. The code inside the flow builder only executes when .collect() is called.
When to use it
Use Flow for any stream of data that arrives over time. Three common places are: first, receiving live updates from a database; second, handling data from a websocket or chunked network response; third, processing continuous user input events like text field changes or button clicks in a UI.
When not to use it
If you only need a single value from an asynchronous operation, a regular suspend function is simpler and more direct. For example, fetching a user's profile is a one-shot operation; a suspend fun getUser(): User is a better fit than a Flow<User> that only emits once and then completes.
One canonical example
A flow that emits three numbers, waiting 100ms between each. The producer uses a flow builder with a loop that calls delay(100) and then emit(i). The consumer calls .collect { value -> println(value) }. While the flow is being collected, other coroutines can run concurrently, proving that the flow's delay does not block the main thread.
Interview question
What happens if a Kotlin Flow is created but no terminal operator like .collect() is invoked?
- a.The Flow will buffer all values until a collector eventually attaches.
- b.The Flow's producer code will not execute, and no values will be emitted.Correct
- c.The Flow will emit values, but they will be discarded without a collector.
- d.The application will encounter a runtime error due to an unhandled stream.
Why? this is the answer
Flows are 'cold,' meaning their producer code only executes when a terminal operator like .collect() is called. Without a collector, the flow builder block never runs, so no values are emitted. Option C is incorrect because nothing is emitted to be discarded.
Just read this? Test yourself on what you have been reading.
Read the original → kotlinlang.org
You just looked this up. Could you explain it out loud?
That is the part interviews actually test. Tezvyn takes questions like this one and gives you what the interviewer is really checking, the answer that lands, and the mistake that ends the conversation, in the four minutes before your next meeting.
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.
We are hiring for this. Open roles that interview on kotlin — each one lists the topics its interview covers.
See open roles