【问题标题】:Koin Android: org.koin.error.NoBeanDefFoundException when Inject RepositoryKoin Android:注入存储库时出现org.koin.error.NoBeanDefFoundException
【发布时间】:2019-11-23 17:50:02
【问题描述】:

我使用 mvvm 模式和 koin 为 DI 制作项目,但我总是没有找到定义存储库

我已经在视图模型之前在模块应用程序中定义了存储库,但是我得到了一些错误

Gradle 应用程序

// Koin for Android
implementation "org.koin:koin-android:$rootProject.koin_version"
// Koin Android Scope features
implementation "org.koin:koin-androidx-scope:$rootProject.koin_version"
// Koin Android ViewModel features
implementation "org.koin:koin-androidx-viewmodel:$rootProject.koin_version"

模块

val dataModule = module {
//remoteData
single { AppRemoteData() }

//repository
single{ AppRepository(get()) as AppDataSource}

// viewmodel
viewModel{ ListHomeViewModel(get()) }
viewModel { LoginViewModel(get()) }

定义模块

val myAppModule = listOf(appModule, dataModule)

在应用中

startKoin {
        androidLogger(Level.DEBUG)
        androidContext(this@MainApp)
        modules(myAppModule)
    }

存储库类

class AppRepository(val appRemoteDataSource: AppRemoteData) : AppDataSource {

override fun loginAccount(email: String, password: String) : LiveData<String> {
    val data = MutableLiveData<String>()
        appRemoteDataSource.loginAccount(email,password,object : AppDataSource.LoginCallback{
            override fun onSucces(id: String?) {
                //berhasil
                data.postValue(id)
            }

            override fun onFailed(message: String?) {
                data.postValue(message)
                d(message)
            }

            override fun onFailure(message: String?) {
                data.postValue(message)
                d(message)
            }

        })
    return  data
}

AppRemoteData

class  AppRemoteData  {
val ref = FirebaseDatabase.getInstance().getReference("user")
var auth = FirebaseAuth.getInstance()

 fun loginAccount(email: String, password: String, callback: AppDataSource.LoginCallback) {
    auth.signInWithEmailAndPassword(email, password)
        .addOnCompleteListener {
            task ->
            if(task.isComplete){
                callback.onSucces(task.result?.user?.providerId)
            }else{
                callback.onFailed(task.exception?.localizedMessage)
            }
        }
}}

这里是错误信息

【问题讨论】:

  • 你能把你的LoginViewModel代码贴在这里吗?

标签: android kotlin mvvm koin


【解决方案1】:

如果在您的 ViewModel 中有一个接口作为参数。

class MyViewModel(val interfaceName: InterfaceName) : ViewModel()

在你的模块定义中使用 singleBy 而不是 Single()。

singleBy<InterfaceName,InterfaceNameImplementation>()

终于进入你的 ViewModel

viewModel { MyViewModel(get()) }

这在 Koin 2.0 中对我有用

希望对你有帮助:)

【讨论】:

    【解决方案2】:

    错误消息告诉您 Koin 无法为您创建 LoginViewModel 实例,因为它必须在创建过程中提供 AppRepository 的实例,但您没有告诉它如何这样做。

    我的猜测是您不小心直接在 LoginViewModel 构造函数中使用了 AppRepository 类型,而不是使用您在模块中绑定存储库实例的 AppDataSource

    所以如果你有这样的东西,那将需要一个 AppRepository 特别是:

    class LoginViewModel(val dataSource: AppRepository) : ViewModel()
    

    你应该用这个替换它,你只要求 Koin 提供一个AppDataSource,你确实将它配置为能够提供:

    class LoginViewModel(val dataSource: AppDataSource) : ViewModel()
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-06-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-08-19
      • 1970-01-01
      • 2019-12-29
      相关资源
      最近更新 更多