kotlinkotlin-multiplatformkoin

Inject class instance in a viewmodel using by koin inject()


I have a strange error in my code which might be a limitation by koin but I don't find any information on that so I am asking here.

This is my viewmodel:

class LoginViewModel:
    BaseViewModel<LoginViewModelContract.State, LoginViewModelContract.Event>() {

    private val authProvider: AuthProvider by inject()
    ...
}

BaseViewModel is a simple class that implements androidx.lifecycle.ViewModel.

The issue here is that inject() can not be imported by koin. Does anybody know why? My setup of koin is correct, I can inject instances in all my other classes but not in this viewmodel.

Intellij shows the following error message:

Unresolved reference. None of the following candidates is applicable because of receiver type mismatch: public inline fun ComponentCallbacks. inject(qualifier: Qualifier? = ..., mode: LazyThreadSafetyMode = ..., noinline parameters: ParametersDefinition? /* = (() → ParametersHolder)? */ = ...): Lazy<TypeVariable(T)> defined in org. koin. android. ext. android


Solution

  • the LoginViewModel needs to implement the KoinComponent interface

    class LoginViewModel:
    BaseViewModel<LoginViewModelContract.State, LoginViewModelContract.Event>(),KoinComponent