Kotlin Flow Basics
Emit and collect streams of data.
Kotlin Flow Basics is a free Android Academy lesson on CoddyKit — lesson 3 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Android Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
What Is a Flow?
A Flow is an asynchronous stream that emits multiple values over time. Think of it as a suspendable sequence you can transform and collect.
Building a Flow
The flow { } builder lets you emit values, optionally suspending between them.
fun numbers(): Flow<Int> = flow {
for (i in 1..3) {
delay(100)
emit(i)
}
}Collecting a Flow
A flow does nothing until you collect it. Collection is a suspend operation, so it runs in a coroutine.
scope.launch {
numbers().collect { value ->
println(value)
}
}Cold Streams
Flows are cold: the builder block runs fresh for each collector. Two collectors each get their own independent sequence from the start.
Transforming with map and filter
Operators transform emissions lazily, only running when collected.
numbers()
.map { it * 10 }
.filter { it > 10 }
.collect { println(it) }More Operators
take(n)— only the first n values.onEach— side effect per value.distinctUntilChanged— skip repeats.
Handling Errors
Catch upstream exceptions with the catch operator.
numbers()
.map { riskyTransform(it) }
.catch { e -> emit(-1) }
.collect { println(it) }Reacting to Completion
Use onCompletion to run code when the flow finishes or is cancelled.
numbers()
.onCompletion { println("done") }
.collect { println(it) }Changing the Producer Thread
Use flowOn to run the upstream on a specific dispatcher without affecting the collector.
fun load(): Flow<Data> = flow {
emit(readFromDisk())
}.flowOn(Dispatchers.IO)Terminal Operators
Some operators end the flow and return a value, like toList, first, or reduce. They are suspend functions.
val all: List<Int> = numbers().toList()Where Flows Shine in Android
Flows model anything that emits over time: database changes (Room), location updates, search-as-you-type, and combining multiple data sources.
Quick Check
Test your understanding of Flow basics.
Recap
You learned:
flow { emit(...) }builds an async stream.- Flows are cold and only run when collected.
- Transform with
map/filter, handle errors withcatch. flowOnsets the producer dispatcher.
Frequently asked questions
Is the “Kotlin Flow Basics” lesson free?
Yes — the full text of “Kotlin Flow Basics” is free to read here on the web, and the Android Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Android Academy course, upgrade to CoddyKit PRO.
What will I learn in “Kotlin Flow Basics”?
Emit and collect streams of data. You practise Android Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Android Academy?
No prior experience is required. Android Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 3 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Kotlin Flow Basics” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Android Academy lesson?
Yes. Every Android Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.