Dagger Kotlin限定符构造函数注入不起作用

问题描述 投票:0回答:1

我使用带有限定符的@Provides方法跟随模块

@Module
class VocabularyModule {

    @VocabularyProviders
    @Singleton
    @Provides
    fun provideVocabularies(): List<VocabularyProvider> {
        return listOf(
                AnimalsVocabularyProvider(),
                FamilyVocabularyProvider(),
                FoodVocabularyProvider(),
                NumberVocabularyProvider(),
                ColorsVocabularyProvider(),
                FreeTimeVocabularyProvider(),
                SportVocabularyProvider(),
                NatureVocabularyProvider(),
                PeopleVocabularyProvider(),
                TransportationVocabularyProvider()
        )
    }
}

@Qualifier
@Retention(AnnotationRetention.RUNTIME)
annotation class VocabularyProviders

然后有我的类,我想将此列表注入via构造函数和限定符:

class VocabularyFactory
@Inject constructor(@param:VocabularyProviders val providers: List<VocabularyProvider>) {

    fun getVocabulary(category: VocabularyCategory): Vocabulary {
        for (provider in providers) {
            if (category == provider.category) {
                return provider.vocabulary
            }
        }
        throw IllegalStateException("didn't find provider that could provide vocabulary of $category category")
    }

}

我收到此错误,但一切看起来都正确

11: error: [Dagger/MissingBinding] [dagger.android.AndroidInjector.inject(T)] @cz.ejstn.learnlanguageapp.core.dagger.module.vocabulary.VocabularyProviders java.util.List<? extends cz.ejstn.learnlanguageapp.vocabulary.model.factory.VocabularyProvider> cannot be provided without an @Provides-annotated method.
android kotlin dagger
1个回答
2
投票

我继续翻阅类似的问题并遇到了这个问题:Dagger 2 multibindings with Kotlin

所以从我的理解kotlin编译器“混乱”与参数中的泛型类型,这导致匕首无法链接这些我猜。

我改变了工厂的构造函数,以避免这种情况 - addind @JvmSuppressWildcards注释:

class VocabularyFactory
@Inject constructor(@param:VocabularyProviders val providers:@JvmSuppressWildcards List<VocabularyProvider>) {
...
}

保持这个问题,因为更多的人可能会遇到这个问题

© www.soinside.com 2019 - 2024. All rights reserved.