Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading