Dagger Hilt:如果没有 @Provides-annotated 方法,则无法提供存储库

Dagger Hilt: Repository cannot be provided without an @Provides-annotated method

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

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