Dagger / MissingBinding。如果没有@Provides注释的方法,则无法提供输出器

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

我正在通过他们的教程学习Dagger2,但在part 5被阻止。我已经在Kotlin(jvm)中实现了所有内容,并且逻辑/行为与工作中的教程相符。

但是,我无法创建SystemOutModule,并且不确定我是否误将Dagger或在翻译其模块类时弄错了。

/**
* Tutorial's Java.
*/
@Module
abstract class SystemOutModule {
    @Provides
    static Outputter textOutputter() {
        return System.out::println;
    }
}
/**
* My Kotlin.
*/
@Module
object SystemOutModule {
    @JvmSuppressWildcards
    @Provides
    fun textOutputter(): (String) -> Unit = { println(it) }
}

构建失败,出现此错误:

~~ ./gradlew clean build


> Task :app:kaptKotlin FAILED
e: /Users/eric/IdeaProjects/atm/app/build/tmp/kapt3/stubs/main/com/es0329/atm/CommandRouterFactory.java:7: error: [Dagger/MissingBinding] com.es0329.atm.Outputter cannot be provided without an @Provides-annotated method.
public abstract interface CommandRouterFactory {
                ^
      com.es0329.atm.Outputter is injected at
          com.es0329.atm.HelloWorldCommand(outputter)
      com.es0329.atm.HelloWorldCommand is injected at
          com.es0329.atm.HelloWorldModule.helloWorldCommand(command)
      com.es0329.atm.Command is injected at
          com.es0329.atm.CommandRouter(command)
      com.es0329.atm.CommandRouter is provided at
          com.es0329.atm.CommandRouterFactory.router()

FAILURE: Build failed with an exception.

我正在通过他们的教程学习Dagger2,但是在第5部分中受阻。我已经在Kotlin(jvm)中实现了所有功能,并且逻辑/行为在此方面与该教程相匹配。但是,我是...

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

您的HelloWorldCommand需要一个Outputter,但您提供的却是(String) -> Unit。有几种方法可以弥合这种差距:

© www.soinside.com 2019 - 2024. All rights reserved.