Skip to content

Instantly share code, notes, and snippets.

@alome007
Created January 17, 2023 12:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alome007/03ab26e0171fd558b1dddd100ce5811d to your computer and use it in GitHub Desktop.
Save alome007/03ab26e0171fd558b1dddd100ce5811d to your computer and use it in GitHub Desktop.
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