Flow Basics in Shared Code
Emit a stream of values from commonMain.
Flow Basics in Shared Code is a free Kotlin Multiplatform Academy lesson on CoddyKit — lesson 1 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 Kotlin Multiplatform Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
Streams of Values
A suspend function returns one value, then it is done. A Flow is different: it emits a whole stream of values over time, one after another. 🌊
Cold by Default
A Flow is cold: the code inside it does nothing until someone starts collecting. No collector means no work, which keeps your shared code lazy and cheap.
Build One with flow {}
The flow builder is the simplest way to create a stream. Inside it you call emit to push each value out to whoever is listening.
fun ticks(): Flow<Int> = flow {
emit(1)
emit(2)
emit(3)
}emit Pushes a Value
Each emit call hands one value to the collector before moving on. The stream is finished only when the builder block returns.
Collect to Receive
To read a Flow you collect it. Collecting is a suspend call, so it lives inside a coroutine and runs your block for every emitted value.
ticks().collect { value ->
println(value)
}It Works Everywhere
Flow ships in kotlinx-coroutines, which is multiplatform. The exact same flow code in commonMain runs on Android and iOS with zero changes. 🎉
flowOf for Fixed Values
When you already know the values, flowOf wraps them into a Flow instantly. It is the quickest stream you can make.
val names = flowOf("Ana", "Beto", "Cem")Turn a List into a Flow
Any Iterable can become a stream with asFlow. This is handy when you already have a list and want to process it one item at a time.
val stream = listOf(10, 20, 30).asFlow()Emit Over Time
Because the builder is a suspend block, you can delay between emissions. That models real streams like ticks, sensor readings, or polling.
flow {
repeat(3) { i ->
delay(1000)
emit(i)
}
}Flow vs suspend
Reach for a suspend function for a single async result, and a Flow when values keep arriving. Choosing the right one keeps your API honest.
Why It Fits Shared Code
Flow lets your shared layer push data as it changes, and each app just listens. Your business logic drives the UI without knowing which platform it is on.
Quick Check
Let us make sure the basics stuck.
Recap
You met Flow: a cold stream you build with flow and read with collect. It is multiplatform, so the same stream feeds Android and iOS alike. 🌊
Frequently asked questions
Is the “Flow Basics in Shared Code” lesson free?
Yes — the full text of “Flow Basics in Shared Code” is free to read here on the web, and the Kotlin Multiplatform 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 Kotlin Multiplatform Academy course, upgrade to CoddyKit PRO.
What will I learn in “Flow Basics in Shared Code”?
Emit a stream of values from commonMain. You practise Kotlin Multiplatform 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 Kotlin Multiplatform Academy?
No prior experience is required. Kotlin Multiplatform Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 1 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Flow Basics in Shared Code” 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 Kotlin Multiplatform Academy lesson?
Yes. Every Kotlin Multiplatform 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.
All lessons in this course
- Flow Basics in Shared Code
- StateFlow for Shared UI State
- Operators: map, filter, combine
- Collect Flows on Android & iOS