i was trying to inject usecase with having one parameter using koin dependency injection, and got error Cannot create an instance of class viewmodel, could you please help me how to inject such usecase in android using koin.
My usecase class
class DetailsUseCase(private val repository: Repository) {
operator fun invoke(id: Int) = repository.getDetails(id)
}
my viewModel Class
class DetailsViewModel(useCase: DetailsUseCase) : ViewModel() {
fun getDetails(id: Int) = useCase(id)
}
my error as follows -
java.lang.RuntimeException: Cannot create an instance of class com.example.package.presentation_layer.DetailsViewModel
Caused by: java.lang.NoSuchMethodException: com.example.package.presentation_layer.DetailsViewModel.<init> []
at java.lang.Class.getConstructor0(Class.java:3325)
at java.lang.Class.getDeclaredConstructor(Class.java:3063)
at androidx.lifecycle.ViewModelProvider$NewInstanceFactory.create(ViewModelProvider.kt:199)
You need to add the view model to your module. For example:
val appModule = module {
viewModel {
DetailsViewModel(
useCase = get(),
)
}
}
and then pass appModule to the modules list when calling startKoin.