kotlin 匕首柄:如果没有@ Provides注解的方法,则无法提供存储库

dba5bblo  于 2023-01-05  发布在  Kotlin
关注(0)|答案(3)|浏览(121)

Hilt指出,如果没有@Provides注解,就不能提供这个接口:

interface PlannedListRepository {
    fun getAllLists(): LiveData<List<PlannedList>>

    suspend fun addList(plannedList: PlannedList)

    suspend fun updateList(plannedList: PlannedList)

    suspend fun deleteList(plannedList: PlannedList)
}

接口的实现:

class PlannedListRepositoryImpl @Inject constructor(private val plannedListDao: PlannedListDao) :
    PlannedListRepository {
    ...
}

据我所知,如果我想得到一个接口,我可以用@Binds告诉Dagger应该接收哪个实现,所以我这样做了:

@Module
@InstallIn(ActivityComponent::class)
abstract class RepositoryModule {

    @Binds
    abstract fun providePlannedListRepository(impl: PlannedListRepositoryImpl) : PlannedListRepository
}

我的ViewModel,如果它有处理错误的东西:

@HiltViewModel
class PlannedListViewModel @Inject constructor(
    private val repository: PlannedListRepository
    ) : ViewModel() {

    ...

}

那么我应该怎么做来修复错误呢?完整的错误消息:

AndroidStudioProjects\PlanShopping\app\build\generated\hilt\component_sources\debug\com\tetsoft\planshopping\PlannerApplication_HiltComponents.java:129: error: [Dagger/MissingBinding] com.tetsoft.planshopping.db.planned.PlannedListRepository cannot be provided without an @Provides-annotated method.
  public abstract static class SingletonC implements PlannerApplication_GeneratedInjector,
                         ^
 com.tetsoft.planshopping.db.planned.PlannedListRepository is injected at
          com.tetsoft.planshopping.ui.planned.PlannedListViewModel(repository)
      com.tetsoft.planshopping.ui.planned.PlannedListViewModel is injected at
          com.tetsoft.planshopping.ui.planned.PlannedListViewModel_HiltModules.BindsModule.binds(vm)
      @dagger.hilt.android.internal.lifecycle.HiltViewModelMap java.util.Map<java.lang.String,javax.inject.Provider<androidx.lifecycle.ViewModel>> is requested at
          dagger.hilt.android.internal.lifecycle.HiltViewModelFactory.ViewModelFactoriesEntryPoint.getHiltViewModelMap() [com.tetsoft.planshopping.PlannerApplication_HiltComponents.SingletonC ? com.tetsoft.planshopping.PlannerApplication_HiltComponents.ActivityRetainedC ? com.tetsoft.planshopping.PlannerApplication_HiltComponents.ViewModelC]

下面是数据库模块:

@Module
@InstallIn(SingletonComponent::class)
class DatabaseModule {

    @Provides
    @Singleton
    fun provideDatabase(@ApplicationContext appContext: Context) : PlannerDatabase {
        return Room.databaseBuilder(
            appContext,
            PlannerDatabase::class.java,
            "PlannerDB"
        )
            .fallbackToDestructiveMigration()
            .build()
    }

    @Provides
    fun providePlannedListDao(plannerDatabase: PlannerDatabase) : PlannedListDao {
        return plannerDatabase.plannedListDao()
    }

    @Provides
    fun provideProductDao(plannerDatabase: PlannerDatabase) : ProductDao {
        return plannerDatabase.productDao()
    }

    @Provides
    fun provideSelectedProductDao(plannerDatabase: PlannerDatabase) : SelectedProductDao {
        return plannerDatabase.selectedProductDao()
    }
}
nafvub8i

nafvub8i1#

在存储库模块中使用@InstallIn(ViewModelComponent::class),因为您是视图模型中的注入存储库

vof42yt1

vof42yt12#

你的应用程序是多模块的吗?如果你的答案是肯定的,请检查你从你的:app中的导入,在我的情况下,我面临着同样的错误,我忘记了添加我的其他模块,在那里定义了DI!

bmp9r5qi

bmp9r5qi3#

迟做总比不做好。
此外,你需要做的事情提到的接受的答案。如果你是与compose或最新的Hilt版本,初始化您的ViewModel的方式:
第一个月
不使用以下方式:
@Inject lateinit var viewModel: YourViewModel

相关问题