0

I have following module with @Provides method with a qualifier

@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

Then there is my class that I want to inject this list into via constructor and the qualifier:

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")
    }

}

I am getting this error but everything looks correct

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.
estn
  • 1,203
  • 1
  • 12
  • 25
  • I have gone through other questions - finding out that in Kotlin you need to specify where exactly should injection happen, leafing through http://kotlinlang.org/docs/reference/annotations.html#annotation-use-site-targets @param seemed to be most suitable for my problem but it didnt work – estn Sep 08 '18 at 17:29

1 Answers1

3

I continued leafing through similar questions and came across this one: Dagger 2 multibindings with Kotlin

So from my understanding kotlin compiler "messes a bit" with generic types in parameters, which resulted in dagger being unable to link these I guess.

I changed constructor of the factory like this to avoid that - addind @JvmSuppressWildcards annotation:

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

Keeping this question here because more ppl are likely to run into this

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
estn
  • 1,203
  • 1
  • 12
  • 25