Predict the output
How many times is 'start' printed?
fun main() = runBlocking {
val numbers = flow {
println("start")
emit(1)
}
numbers.collect { }
numbers.collect { }
}Hint 1 of 1
When does the code inside flow { } run?
Choose an answer, then open Show answer below to check it.
Show answer
Answer: A Twice
Correct. A cold flow runs its block once per collector.
Why not the others?
- B Once. That would be a shared, hot flow.
- C Never, because nothing is printed inside collect. The flow block itself prints, and it runs when collected.
- D Once when the flow is created, then never. Creating a flow runs nothing; collecting does.