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



我有以下模块@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

然后是我的类,我想通过构造函数和限定符将此列表注入其中:

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.

我继续翻阅类似的问题,遇到了这个问题: Dagger 2 与 Kotlin 的多重绑定

因此,根据我的理解,kotlin 编译器与参数中的泛型类型"有点混乱",这导致 dagger 无法链接这些我猜。

我像这样更改了工厂的构造函数以避免这种情况 - 添加@JvmSuppressWildcards注释:

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

把这个问题留在这里,因为更多的人可能会遇到这个问题

最新更新