Complete the code to create a MutableStateFlow with initial value 0.
val counter = MutableStateFlow([1])MutableStateFlow requires an initial value of the correct type. Here, 0 is an integer initial value.
Complete the code to update the StateFlow value by incrementing it by 1.
counter.value = counter.value [1] 1
To increase the counter by 1, use the plus operator '+'.
Fix the error in the code to collect StateFlow values in a coroutine.
lifecycleScope.launch {
counter.[1] { value ->
println("Counter: $value")
}
}To observe StateFlow values, use the collect function inside a coroutine.
Fill both blanks to create a StateFlow that only emits even numbers from a MutableStateFlow.
val evenNumbers = counter.[1] { it % 2 == 0 }.[2]()
Use filter to keep only even numbers, then stateIn to convert the flow back to StateFlow.
Fill all three blanks to create a StateFlow with initial value 0, update it, and collect its values.
val counter = MutableStateFlow([1]) counter.value = counter.value [2] 5 lifecycleScope.launch { counter.[3] { value -> println("Value: $value") } }
Initialize with 0, add 5 to update, and collect values to observe changes.