Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there Flow's version of flatMapIterable?

I have started using Kotlin Flow. Now I'm struggling to map elements from a list, returned by api, one by one. Is there an operator available that does something like RxJava's flatMapIterable?

For example, when the Flow has an ArrayList<T>, then I want to make an operation on every element of that list, and get it as a new Flow.

like image 950
tim_baton Avatar asked Sep 13 '25 22:09

tim_baton


1 Answers

You can use flatMapMerge operator to achieve the desired result. You can convert a Flow<List<T>> to Flow<T> this way:

yourFlow.
   flatMapMerge { it.asFlow() }

This way you can further process the stream from every element of the initial list using the default Flow operators.

Here is the doc: flatMapMerge

like image 131
Gaket Avatar answered Sep 16 '25 13:09

Gaket