Dagger2和Kotlin:@Binds不适用于@IntoMap

问题描述 投票:0回答:1

我正在从这里上课: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吗?

kotlin dependency-injection dagger-2 dagger
1个回答
0
投票

谢谢@David Medenjak,您是对的!上面的代码还可以,问题是缺少@JvmSuppressWildcards,所以我的课程CommandRouter现在看起来像:

@JvmSuppressWildcards
class CommandRouter @Inject constructor(
    val outputter: Outputter,
    val commands: Map<String, Command>
) {
// ...
}
© www.soinside.com 2019 - 2024. All rights reserved.