Koin: by viewModel() gives compilation error

Created on 11 Sep 2018  路  5Comments  路  Source: InsertKoinIO/koin

Describe the bug

getting compilation error: Expression 'viewModel' of type 'PatientViewModel' cannot be invoked as a function. The function 'invoke()' is not found

Koin project used and used version (please complete the following information):

koin-androidx-viewmodel version 1.0.0-RC-1

module definition

val appModule = module {
    viewModel { PatientViewModel(get()) }
  ....
}

fragment definition

class PatientFragment : Fragment()
{
    val viewModel: PatientViewModel by viewModel()
...
}

viewModel definition

import androidx.lifecycle.LiveData
import androidx.lifecycle.MediatorLiveData
import androidx.lifecycle.ViewModel

class PatientViewModel(val patientRepo: PatientRepository) : ViewModel() {
    private val patient = MediatorLiveData<Patient>()
...

Most helpful comment

fixed with private val viewModel by viewModel<PatientViewModel>()

All 5 comments

fixed with private val viewModel by viewModel<PatientViewModel>()

fixed with private val viewModel by viewModel<PatientViewModel>()

I am getting Unresolve reference: viewModel with that

fixed with private val viewModel by viewModel<PatientViewModel>()

I am getting Unresolve reference: viewModel with that

Try import this

import org.koin.android.viewmodel.ext.android.viewModel

Inside the fragment use 'by sharedViewModel()' instead of 'by viewModel()'

fixed with private val viewModel by viewModel<PatientViewModel>()

I am getting Unresolve reference: viewModel with that

Try import this

import org.koin.android.viewmodel.ext.android.viewModel

AndroidX users having the same problem can just use
import org.koin.androidx.viewmodel.ext.android.viewModel

(Notice the bold x)

Was this page helpful?
0 / 5 - 0 ratings

Related issues

caleb-allen picture caleb-allen  路  4Comments

hkelidari picture hkelidari  路  3Comments

erikhuizinga picture erikhuizinga  路  3Comments

haroldadmin picture haroldadmin  路  3Comments

CristianMG picture CristianMG  路  3Comments