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>()
...
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)
Most helpful comment
fixed with
private val viewModel by viewModel<PatientViewModel>()