Dagger: Problem with Multibinding - cannot be provided without an @Provides-annotated method.

Created on 5 Oct 2019  路  2Comments  路  Source: google/dagger

I'm trying to use the multi binding, but i'm getting the following error:

error: [Dagger/MissingBinding] java.util.Map<java.lang.String,? extends com.snazhmudinov.todo.Storage> cannot be provided without an @Provides-annotated method.

Code snippets:

interface Storage {

    fun get(): String

    fun set(t: String)
}

@Singleton
class StringStorage @Inject constructor() : Storage {

    override fun get(): String {
        return ""
    }

    override fun set(t: String) {
    }
}

@Singleton
class String2Storage @Inject constructor() : Storage {

    override fun get(): String {
        return ""
    }

    override fun set(t: String) {
    }
}

@Singleton
class StorageProvider @Inject constructor(val storages: Map<String, Storage>)

Module declaration:

@Module
abstract class StorageModule {

    @IntoMap
    @Binds
    @StringKey("StringStorage")
    abstract fun bindStringStorage(storage: StringStorage): Storage

    @IntoMap
    @Binds
    @StringKey("String2Storage")
    abstract fun bindString2Storage(storage: String2Storage): Storage
}

Component:

@Singleton
@Component(modules = [
    AndroidSupportInjectionModule::class,
    ViewModelModule::class,
    AppModule::class,
    DbModule::class,
    RepoModule::class,
    ActivityModule::class,
    ServiceModule::class,
    DispatcherModule::class,
    StorageModule::class
])
interface ToDoComponent : AndroidInjector<ToDoApplication> {

    @Component.Factory
    interface Factory {
        fun create(@BindsInstance appContext: Context): ToDoComponent
    }
}

And finally, the activity injection:

@Inject
lateinit var storageProvider: StorageProvider

Kotlin version: 1.3.50
Dagger version: 2.24
AS version: 3.5

Most helpful comment

You need to use @JvmSuppressWildcards; see https://github.com/google/dagger/issues/900#issuecomment-337065004

All 2 comments

You need to use @JvmSuppressWildcards; see https://github.com/google/dagger/issues/900#issuecomment-337065004

thank you 馃檹

Was this page helpful?
0 / 5 - 0 ratings

Related issues

SteinerOk picture SteinerOk  路  3Comments

6bangs picture 6bangs  路  3Comments

makaroffandrey picture makaroffandrey  路  3Comments

SAGARSURI picture SAGARSURI  路  3Comments

matpag picture matpag  路  3Comments