Skip to content

Instantly share code, notes, and snippets.

@alome007
Created January 17, 2023 12:15
Embed
What would you like to do?
Viewmodel Demonstrating a simple counter using stateflow
import androidx.lifecycle.ViewModel
import kotlinx.coroutines.flow.MutableStateFlow
// import androidx.lifecycle.ViewModel
// import kotlinx.coroutines.flow.MutableStateFlow
//extending the ViewModel class from androidx.lifecycle package
class MyViewModel : ViewModel() {
// property counter with initial value 0
val counter = MutableStateFlow(0)
// function to increment the counter value
fun incrementCounter() {
counter.value++ // increment the value of counter by 1
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment