I am using dagger 2 (version 2.15) for dependency injection in my android project. Everything works fine except injecting viewModelFactory. I cannot inject it in my fragment.
@Inject
lateinit var viewModelFactory: ViewModelProvider.Factory
private val mainViewModel: StationsViewModel by lazy {
ViewModelProviders.of(this, viewModelFactory)
.get(StationsViewModel::class.java)
}
I get the following error:
lateinit property viewModelFactory has not been initialized
I have ViewModelModule:
@Module
abstract class ViewModelModule {
@Binds
@IntoMap
@ViewModelKey(MainViewModel::class)
internal abstract fun bindsMainViewModel(mainViewModel:
MainViewModel): ViewModel
@Binds
abstract fun bindViewModelFactory(factory: MyViewModelFactory):
ViewModelProvider.Factory
}
ViewModelKey
@MustBeDocumented
@Target(
AnnotationTarget.FUNCTION,
AnnotationTarget.PROPERTY_GETTER,
AnnotationTarget.PROPERTY_SETTER
)
@Retention(AnnotationRetention.RUNTIME)
@MapKey
annotation class ViewModelKey(val value: KClass<out ViewModel>)
and ViewModelFactory
@Singleton
class MyViewModelFactory @Inject constructor(
private val creators: Map<Class<out ViewModel>, @JvmSuppressWildcards Provider<ViewModel>>
) : ViewModelProvider.Factory {
override fun <T : ViewModel> create(modelClass: Class<T>): T {
val creator = creators[modelClass] ?: creators.entries.firstOrNull {
modelClass.isAssignableFrom(it.key)
}?.value ?: throw IllegalArgumentException("unknown model class $modelClass")
try {
@Suppress("UNCHECKED_CAST")
return creator.get() as T
} catch (e: Exception) {
throw RuntimeException(e)
}
}
}
although I do not quite understand what they are doing.
Can you try to initiate in onViewCreated() ?
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
mainViewModel = ViewModelProviders.of(this, viewModelFactory)
.get(StationsViewModel::class.java)
// binding.viewmodel = viewModel
}
Should be okay.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With