首页 > 解决方案 > 提供方法必须返回一个值(非 void)

问题描述

当我尝试使用 Dagger 2 时出现此错误,我该怎么办,我在 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) {
 

模块 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()
  }
}

界面

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

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

标签: javaandroidkotlindagger-2

解决方案


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


推荐阅读