Kotlin Flow - is there anything similar to LiveData's emitSource?

Viewed 245

I have a function that takes a parameter function returning a 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

   ...
   }

Is there anything that can be done to flow to be able to be sent like this?

1 Answers

If you want to emit all items from a Flow, that's exactly what emitAll() does:

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)
}
Related