Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Where is the "viewModel()" method in Androids Compose State documentation coming from?

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..

enter image description here

like image 590
Kimble Avatar asked Sep 09 '25 17:09

Kimble


2 Answers

You can check the official doc:

you can access a ViewModel from any composable by calling the viewModel() function.

To use the viewModel() functions,add the androidx.lifecycle:lifecycle-viewmodel-compose:$latestVersion dependency to your build.gradle file.

Finally use:

import androidx.lifecycle.viewmodel.compose.viewModel
like image 84
Gabriele Mariotti Avatar answered Sep 13 '25 08:09

Gabriele Mariotti


Obviously figured it out 2 minutes after giving up and posting the question.

You need this dependency:

androidx.lifecycle:lifecycle-viewmodel-compose:1.0.0-alpha05
like image 33
Kimble Avatar answered Sep 13 '25 09:09

Kimble