Kotliin Dagger Field Injection in ViewModel Throws Dagger/Binding Exception

牧云@^-^@ 提交于 2019-12-22 17:49:22

问题


I have followed this tutorial in order to do DI in my viewmodels. But I currently am stuck.

I have created a ViewModelFactory for my viewmodel which is as follows:

class HomeViewModelFactory @Inject constructor(
    private val creators: Map<Class<out ViewModel>,
            Provider<ViewModel>>
): ViewModelProvider.Factory{

    override fun <T : ViewModel?> create(modelClass: Class<T>): T {
        return creators[modelClass]?.get() as T
    }
}

Followed by a ViewModel:

class HomeViewModel @Inject constructor(private val songsRepository: SongsRepository): ViewModel()

For DI I have created two components. One is my main application component, the other is a component which depends on the main application.

@Singleton
@Component(modules = [AppModule::class])
public interface AppComponent {
    fun songRepository(): SongsRepository
    fun libraryManager(): LibraryManager
    fun inject(mainActivity: MainActivity)
}


@Module
public class AppModule(val application: Application){

    @Provides @Singleton
    fun providesApplication(): Application{
        return application
    }

    @Provides @Singleton
    fun providesLibraryManager(): LibraryManager {
        return LibraryManager(application)
    }

    @Provides @Singleton
    fun providesSongRepository(libraryManager: LibraryManager): SongsRepository {
        return SongsRepository(libraryManager)
    }
}

And my ViewModelModule is as follows:

@Module
public class ViewModelModule {

    @Target(AnnotationTarget.FUNCTION)
    @Retention(AnnotationRetention.RUNTIME)
    @MapKey
    internal annotation class ViewModelKey(val value: KClass<out ViewModel>)

    @AppScope
    @Provides
    fun providesHomeViewModelFactory(providerMap: Map<Class<out ViewModel>, Provider<ViewModel>>): HomeViewModelFactory {
        return HomeViewModelFactory(providerMap)
    }

    @AppScope
    @IntoMap
    @Provides
    @ViewModelKey(HomeViewModel::class)
    fun providesHomeViewModel(songsRepository: SongsRepository): HomeViewModel{
        return HomeViewModel(songsRepository)
    }
}

@AppScope
@Component(modules = [ViewModelModule::class], dependencies = [AppComponent::class])
public interface ViewModelComponent {
    fun homeViewModelFactory(): HomeViewModelFactory
    fun homeViewModel(): HomeViewModel
    fun inject(homeFragment: HomeFragment)
}

The error I'm getting is this:

error: [Dagger/MissingBinding] java.util.Map,? extends javax.inject.Provider> cannot be provided without an @Provides-annotated method.

I seriously don't have any idea why is this happening since all my classes have @Inject constructors. Dagger's documentation is not helping me either. I would be grateful if you could advise me on this matter.


回答1:


The error message indicates that the following code is wrong:

fun providesHomeViewModelFactory(providerMap: Map<Class<out ViewModel>, Provider<ViewModel>>): HomeViewModelFactory {
    return HomeViewModelFactory(providerMap)
}

It should be

fun providesHomeViewModelFactory(providerMap: Map<Class<out ViewModel>, @JvmSuppressWildcards Provider<ViewModel>>): HomeViewModelFactory {
    return HomeViewModelFactory(providerMap)
}

It is because the signature of Map interface is Map<K, out V>, that means the Map<..., Provider<ViewModel>> will be compiled to Map<..., ? extends Provider<ViewModel>> Java code, so you are asking dagger for latter one but it only has former one in its object graph, then the compiler throws you the error.



来源:https://stackoverflow.com/questions/57119921/kotliin-dagger-field-injection-in-viewmodel-throws-dagger-binding-exception

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!