Flow: the Basics
KOTLIN › Flow
What a Kotlin Flow actually is: an async stream of values over time, the producer/collector model, emit and collect, cold execution, and why it beats callbacks.
This is the ground floor of everything reactive on Android. Before operators, StateFlow, or combining streams, you need the core picture: a Flow is a stream of values that arrive over time, produced with flow { emit(...) } and consumed with collect { } inside a coroutine. It does nothing until collected, and it's cold, each collector gets its own run. Get comfortable here and the Flow & Operators lesson, which assumes all of this, will actually click.
What this covers
- A suspend function returns one value once; a Flow is a stream of many values arriving over time
- flow { } is the producer and emit() sends values one at a time; collect { } is the consumer whose lambda runs per value
- Builders: flowOf and asFlow when nothing suspends, flow { } when it does, callbackFlow and channelFlow for callbacks and concurrent emission
- A Flow does nothing until a terminal operator like collect() runs it, it is just a description until then
- Terminal operators run the flow: collect, toList, first, single, count, reduce, fold
- first() cancels the upstream, which is what makes it safe on a hot flow; toList() on a hot flow never returns
- Flow and FlowCollector are one-method suspending interfaces, and every operator is a flow wrapping a flow
- Coldness and backpressure both fall out of that shape rather than being implemented features
- Sequence and Flow differ on one axis: a Sequence blocks while waiting, a Flow suspends
- Return a Flow only when there is genuinely more than one meaningful value over time
- A cold flow re-runs its producer independently for every collector; nothing is shared until you go hot
- Flow cancellation: emit() auto-checks, .cancellable() for non-builder flows, ensureActive() for manual checks
Study this topic
- Flow: the Basics explained: the guided lesson
- 19 practice quiz questions
- Flow: the Basics interview questions and answers