Skip to content

Instantly share code, notes, and snippets.

@karanahuja-android
Created April 14, 2025 13:07
Show Gist options
  • Save karanahuja-android/67793edcac8160c93138b03738ef7972 to your computer and use it in GitHub Desktop.
Save karanahuja-android/67793edcac8160c93138b03738ef7972 to your computer and use it in GitHub Desktop.
import kotlinx.coroutines.*
import kotlinx.coroutines.flow.*
fun main() = runBlocking {
val userFlow = MutableStateFlow("User 1")
val settingsFlow = MutableStateFlow("Dark Mode")
// Combine two flows
userFlow.combine(settingsFlow) { user, settings ->
"User: $user, Settings: $settings"
}.collect { combinedData ->
println(combinedData)
}
// Update flows to see combination in action
delay(100)
userFlow.value = "User 2"
delay(100)
settingsFlow.value = "Light Mode"
delay(100)
userFlow.value = "User 3"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment