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
You need to use @JvmSuppressWildcards; see https://github.com/google/dagger/issues/900#issuecomment-337065004
thank you 馃檹
Most helpful comment
You need to use
@JvmSuppressWildcards; see https://github.com/google/dagger/issues/900#issuecomment-337065004