Kotlin Flow - 有没有类似于 LiveData 的 emitSource?



我有一个函数,它接受一个返回Flow、的参数函数

fun <T> resultFlow(
query: () -> Flow<T>,
... other parameters
): Flow<T> {
return flow {
val source = query()
//!!!!    emit(source)   // I want something like emitSource like livedata has
...
}

有什么可以做的事情来流动,以便能够像这样发送吗?

如果您想从流中发出所有项目,emitAll()就是这么做的:

fun <T> resultFlow(
query: () -> Flow<T>,
... other parameters
): Flow<T> {
return flow {
val source = query()
// Note that this will suspend until all values from the flow are collected.
emitAll(source)
}

最新更新