jetpack compose pass parameter to viewModel
Solution 1:
you need to create a factory to pass dynamic parameter to ViewModel like this:
class MyViewModelFactory(private val dbname: String) :
ViewModelProvider.NewInstanceFactory() {
override fun <T : ViewModel?> create(modelClass: Class<T>): T = MyViewModel(dbname) as T
}
then use your factory like this in composable functions:
@Composable
fun UsersList() {
val myViewModel: MyViewModel =
viewModel(factory = MyViewModelFactory("db2name")) // pass param like this
}
and now you have access to dbname parameter in your ViewModel:
class MyViewModel(private val dbname) : ViewModel() {
// ...rest of the viewModel logics here
}
Solution 2:
Usually there is no common case where you need to do this. In android MVVM viewmodels get their data from repositories through dependency injection.
Here is the official documentation to the recommended android architecture: https://developer.android.com/jetpack/guide#recommended-app-arch
Solution 3:
As it was mentioned by @Secret Keeper you need to create factory.
If your ViewModel has dependencies, viewModel() takes an optional ViewModelProvider.Factory as a parameter.
class MyViewModelFactory(
private val dbname: String
) : ViewModelProvider.Factory {
@Suppress("UNCHECKED_CAST")
override fun <T : ViewModel> create(modelClass: Class<T>): T {
if (modelClass.isAssignableFrom(MyViewModel::class.java)) {
return MyViewModel(dbname) as T
}
throw IllegalArgumentException("Unknown ViewModel class")
}
}
To create your viewModel you will pass optional parameter. Inside your Composable you can do something like this.
val viewModel: MyViewModel = viewModel(
factory = MyViewModelFactory(
dbname = "myDbName"
)