Guys imagine I have these two sources of data:
val flowA: Flow<String>
suspend fun funB(): Int
How can I combine the result of both into a flow (let's say Flow<Pair<String, Int>>
)?
How about the approach below? Is there a better way?
combine(
flowA,
flow {emit(funB())}
) { a, b ->
...
}
CodePudding user response:
Assuming you want the same Int
paired with each String in flowA
, you can do it as follows:
val funBResult = funB()
val pairs = flowA.map { it to funBResult }
If funB()
is in fact a function that takes the String
as a parameter, you could do something like:
val pairs = flowA.map { it to funB(it) }