Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 456
padmalcom Avatar asked Apr 29 '26 01:04

padmalcom


1 Answers

the LoginViewModel needs to implement the KoinComponent interface

class LoginViewModel:
BaseViewModel<LoginViewModelContract.State, LoginViewModelContract.Event>(),KoinComponent
like image 118
Mohamed Mabrouki Avatar answered May 03 '26 16:05

Mohamed Mabrouki



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!