🐦 Kotlin Advanced

What is Flow.combine() in Kotlin?

Answer

Flow.combine() merges the latest emissions from multiple Flows using a transform function, emitting a new combined value whenever any of the source Flows emits a new value. combine(flowA, flowB) { a, b -> "$a $b" }. Unlike zip, which waits for both flows to emit before combining, combine always uses the latest value from all other flows. This makes it ideal for combining multiple independent StateFlows in a ViewModel into a single UI state: combine user preferences, loaded data, and loading state into one UiState object that the UI observes.