首页 > 解决方案 > Dagger2 和 Kotlin:@Binds 不适用于 @IntoMap

问题描述

我正在从这里学习:https ://dagger.dev/tutorial/07-two-for-the-price-of-one

当我更改代码时

@Module
abstract class HelloWorldModule {
    @Binds
    abstract fun helloWorldCommand(command: HelloWorldCommand): Command
}

进入

@Module
abstract class HelloWorldModule {
    @Binds
    @IntoMap
    @StringKey("hello")
    abstract fun helloWorldCommand(command: HelloWorldCommand): Command
}

我收到错误:

error: [Dagger/MissingBinding] Map<String,? extends Command> 
cannot be provided without an @Provides-annotated method.

我在这里缺少什么?它不适用于 Kotlin 吗?

标签: kotlindependency-injectiondagger-2dagger

解决方案


谢谢@David Medenjak,你是对的!上面的代码没问题,问题是 missing @JvmSuppressWildcards,所以我的课CommandRouter现在看起来像:

@JvmSuppressWildcards
class CommandRouter @Inject constructor(
    val outputter: Outputter,
    val commands: Map<String, Command>
) {
// ...
}

推荐阅读