How to convert kotlinx.coroutines.flow list to normal data class list.
Since you want to go from a flow of nested lists to a single list, you need a flat-mapping operation:
suspend fun <T> Flow<List<T>>.flattenToList() =
flatMapConcat { it.asFlow() }.toList()
Example of usage:
suspend fun main() {
val flowOfLists: Flow<List<Int>> = flowOf(listOf(1, 2), listOf(3, 4))
val flatList: List<Int> = flowOfLists.flattenToList()
println(flatList)
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With