Viewmodel Demonstrating a simple counter using stateflow
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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