```fun main() = runBlocking { val coroutineScope...
# coroutines
n
Copy code
fun main() = runBlocking {
  val coroutineScope = this

  val mainFlow = flow {
    kotlinx.coroutines.delay(1000)
    emit("String")
  }

  fun createDependentFlows(value: String): Pair<Flow<Int>, Flow<Boolean>> {
    val flow1 = flow {
      emit(value.length)
    }

    val flow2 = flow {
      emit(value.isNotEmpty())
    }

    return Pair(flow1, flow2)
  }

  val combinedFlow = mainFlow.flatMapLatest { mainValue ->
    val (flow1, flow2) = createDependentFlows(mainValue)
    combine(
      flow1,
      flow2
    ) { value1, value2 ->
      Pair(value1, value2)
    }
  }.stateIn(coroutineScope, SharingStarted.Eagerly, null)

  combinedFlow.collect {
    println(it)
  }
}
Why is it that if i add
.stateIn(coroutineScope, SharingStarted.Eagerly, null)
, the combinedFlow will not collect? 🤔
s
Where are you running it? The main difference when you add
stateIn
is that the flow won't terminate. It still emits the values, but after that, the program will run forever. In the Kotlin Playground, the buffering of output could mean that it times out before you see any console output.
n
My real code is running in an android ViewModel, but it still has the same results as if I had run it on Kotlin Playground
s
I'm not sure why that would be. I ran the code in my IDE and I still see the output, whether the
stateIn(…)
line is present or not.
🤔 1
n
Yes, you're right 👍, i just created a JVM project on IDEA to test it and it does output, I'm going to try to prepare a new project for android to test it again