首页 > 解决方案 > Dagger 2 - 提供依赖时出错

问题描述

我对 Dagger 2 真的很陌生,我知道它是如何工作的以及它做了什么,但是我在尝试将它实施到我的项目中时遇到了一些问题。

我现在的目标是将演示者注入我的视图中,目标是解耦我的做事视图

presenter = Presenter(myInteractor())

这是我尝试过的

我的应用程序

class MyAppApplication: Application() {

    lateinit var presentationComponent: PresentationComponent

    override fun onCreate() {
        super.onCreate()
        createPresentationComponent()
    }

    private fun createPresentationComponent() {
        presentationComponent = DaggerPresentationComponent.builder()
            .presentationModule(PresentationModule(this))
            .build()
    }
}

演示组件

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

@Singleton
interface PresentationComponent {

    fun inject(loginActivity: LoginView)
    fun loginUserPresenter(): LoginPresenter
}

演示模块

@Module
class PresentationModule(application: Application) {


    @Provides @Singleton fun provideLoginUserPresenter(signInInteractor: SignInInteractor): LoginPresenter {
        return LoginPresenter(signInInteractor)
    }

}

登录交互器

interface SignInInteractor {

    interface SignInCallBack {
        fun onSuccess()
        fun onFailure(errormsg:String)
    }

    fun signInWithEmailAndPassword(email:String,password:String,listener:SignInCallBack)
    fun firebaseAuthWithGoogle(account: GoogleSignInAccount, listener:SignInCallBack)

}

现在,我认为这就是我需要将交互器注入我的演示者而没有任何问题然后将演示者注入我的视图中所需的全部内容,但是给了我这个错误

error: [Dagger/MissingBinding] com.myapp.domain.interactor.logininteractor.SignInInteractor cannot be provided without an @Provides-annotated method.

我有点困惑,因为如果我只提供负责将我的 signInInteractor 绑定到我的 Presenter 的presentationModule,它应该可以工作,但不是。

提前感谢您的帮助

标签: androidkotlindagger-2dagger

解决方案


正如错误消息所说,您正在尝试将 a 传递给 your SignInInteractor,但您没有在任何地方为其提供实现。一个可能的解决方案是将以下代码块添加到您的:PresentationModuleLoginPresenterPresentationModule

@Provides @Singleton fun provideSignInInteractor(): SignInInteractor {
  return TODO("Add an implementation of SignInInteractor here.")
}

当然,TODO需要用SignInInteractor您选择的 a 替换(myInteractor()例如,该功能可以工作)。然后,SignInInteractor您的LoginPresenter. 希望有帮助!


推荐阅读