Androids compose / state documentation contains a sample referring to a mysterious viewModel()
function or method, but as the documentation has left out all import statements I can't for the life of me figure out where this function is coming from..
You can check the official doc:
you can access a
ViewModel
from any composable by calling theviewModel()
function.
To use the
viewModel()
functions,add theandroidx.lifecycle:lifecycle-viewmodel-compose:$latestVersion
dependency to yourbuild.gradle
file.
Finally use:
import androidx.lifecycle.viewmodel.compose.viewModel