Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Problem with Multibinding - cannot be provided without an @Provides-annotated method. #1631

Closed
snazhmudinov opened this issue Oct 5, 2019 · 2 comments

Comments

@snazhmudinov
Copy link

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

@tbroyer
Copy link

tbroyer commented Oct 5, 2019

You need to use @JvmSuppressWildcards; see #900 (comment)

@snazhmudinov
Copy link
Author

thank you 🙏

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants