提供的方法必须返回一个值(不是 void)

huangapple go评论86阅读模式
英文:

Provides methods must return a value (not void)

问题

以下是翻译好的内容:

当我尝试使用Dagger 2时,出现了以下错误,我应该怎么做?我使用的是Dagger 2新版。

e:/Users/stepanbezhuk/Documents/Work/Rebus/Movie/app/build/tmp/kapt3/stubs/debug/com/popularmovies/movies/Service/MessageWorking.java:11: 错误:@Provides方法必须返回一个值(而不是void)
    public final void messageConnected$app_debug(boolean networkStatus) {

模块1

 @Module
 open class MessageWorking @Inject constructor(private val context: Context) {
 
 @Provides
 internal fun messageConnected(networkStatus: Boolean): String {
      if (networkStatus) {
          showMessage(message = "连接")
      } else {
          showMessage(message = "断开连接")
      }
  }

  private fun showMessage(message: String) {
      Toast.makeText(context, message, Toast.LENGTH_SHORT).show()
  }
}

接口

@Component(modules = arrayOf(MessageWorking::class))

@Singleton
interface DaggerComponent {
    fun messageConnected(networkStatus: Boolean)
}
英文:

When I try to use Dagger 2 it gives this error, what should I do, I'm in Dagger 2 new

e: /Users/stepanbezhuk/Documents/Work/Rebus/Movie/app/build/tmp/kapt3/stubs/debug/com/popularmovies/movies/Service/MessageWorking.java:11: error: @Provides methods must return a value (not void)
    public final void messageConnected$app_debug(boolean networkStatus) {

Module 1

 @Module
 open class MessageWorking @Inject constructor(private val context: Context) {

 @Provides
 internal fun messageConnected(networkStatus: Boolean) {
      if (networkStatus) {
          showMessage(message = "Connect")
      } else {
          showMessage(message = "Disconnect")
      }
  }

  private fun showMessage(message: String) {
      return Toast.makeText(context, message, Toast.LENGTH_SHORT).show()
  }
}

Interface

@Component(modules = arrayOf(MessageWorking::class))

@Singleton
interface DaggerComponent {
    fun messageConnected(networkStatus: Boolean)
}

答案1

得分: 2

@Provides注解表示您必须提供返回内容。在messageConnected()中,您没有返回类型。

您正在Dagger内部执行UI操作(toast),这非常奇怪,因为Dagger的责任是提供依赖关系。

这是一个关于Dagger的简单介绍以及设置方法的链接:
https://medium.com/@skshayne/understanding-dagger-as-a-beginner-8ffe29bad429

英文:

The @Provides annotations mean you must provide something back. you have no return type in messageConnected().

You are doing UI stuff inside Dagger (toast), which is very odd since the dagger's responsibility is to provide dependencies.

Here is a link to a simple intro to Dagger and how to setup:
https://medium.com/@skshayne/understanding-dagger-as-a-beginner-8ffe29bad429

huangapple
  • 本文由 发表于 2020年8月22日 06:11:24
  • 转载请务必保留本文链接:https://go.coder-hub.com/63530647.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定