Complete the code to declare a mutable state variable in ViewModel.
private val _count = MutableStateFlow([1])
val count: StateFlow<Int> = _countWe initialize the mutable state with 0 because count is an integer.
Complete the code to update the state value inside ViewModel.
fun increment() {
_count.value = _count.value [1] 1
}We add 1 to the current value to increment the count.
Fix the error in the ViewModel state declaration.
class CounterViewModel : ViewModel() { private val _count = MutableStateFlow<Int>([1]) val count: StateFlow<Int> = _count }
MutableStateFlow requires a non-null initial value matching the type Int, so 0 is correct.
Fill both blanks to create a function that resets the count to zero.
fun reset() {
_count.[1] = [2]
}We assign 0 to the value property of _count to reset the state.
Fill all three blanks to create a ViewModel with a mutable state and a function to decrement the count.
class CounterViewModel : ViewModel() { private val _count = MutableStateFlow([1]) val count: StateFlow<Int> = [2] fun decrement() { _count.value = _count.value [3] 1 } }
The state starts at 0, the public state is _count, and decrement subtracts 1.
