0
0
Kotlinprogramming~10 mins

Flow builder and collect in Kotlin - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Flow builder and collect
Start Flow Builder
Emit values one by one
Flow created
Call collect on Flow
Collect each emitted value
Process or print value
End when all values emitted
The flow builder creates a flow that emits values one by one. When collect is called, it receives each value and processes it until all are emitted.
Execution Sample
Kotlin
import kotlinx.coroutines.flow.*
import kotlinx.coroutines.runBlocking

fun main() = runBlocking {
    val flow = flow {
        emit(1)
        emit(2)
        emit(3)
    }
    flow.collect { value ->
        println(value)
    }
}
This code creates a flow that emits numbers 1, 2, and 3, then collects and prints each number.
Execution Table
StepActionFlow StateCollected ValueOutput
1Start flow builderFlow created, no values emitted yetNoneNone
2Emit 1Flow emits 11Print 1
3Emit 2Flow emits 22Print 2
4Emit 3Flow emits 33Print 3
5Flow completesNo more valuesNoneCollection ends
💡 All values emitted and collected, flow completes
Variable Tracker
VariableStartAfter Step 2After Step 3After Step 4Final
flowemptyemitted 1emitted 2emitted 3completed
valuenone123none
Key Moments - 2 Insights
Why does collect receive values one by one instead of all at once?
Because the flow emits values sequentially and collect processes each emitted value immediately, as shown in execution_table rows 2 to 4.
What happens if we don't call collect on the flow?
The flow builder creates the flow but no values are emitted or processed until collect is called, so nothing happens (see execution_table step 1 vs steps 2-5).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the collected value at step 3?
A1
B2
C3
DNone
💡 Hint
Check the 'Collected Value' column at step 3 in the execution_table.
At which step does the flow complete and stop emitting values?
AStep 5
BStep 3
CStep 4
DStep 2
💡 Hint
Look for the step where 'Flow completes' is noted in the 'Action' column.
If we remove the collect call, what changes in the execution?
AValues are emitted but not collected
BFlow emits and collects automatically
CNo values are emitted or collected
DValues are collected but not emitted
💡 Hint
Refer to key_moments about what happens if collect is not called.
Concept Snapshot
Flow builder creates a flow that emits values sequentially.
Use emit(value) inside flow { } to send values.
Call collect { value -> ... } to receive and process each emitted value.
Without collect, flow does not emit values.
Collect runs suspending and processes values one by one.
Full Transcript
In Kotlin, a flow builder creates a flow that emits values one at a time using emit(). When you call collect on the flow, it receives each emitted value and processes it, for example by printing. The flow completes after all values are emitted. If you don't call collect, the flow does not emit any values. This step-by-step trace shows how values 1, 2, and 3 are emitted and collected in order.