gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-05 01:53:52 27 4
gpt4 key购买 nike

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


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

suspend fun addList(plannedList: PlannedList)

suspend fun updateList(plannedList: PlannedList)

suspend fun deleteList(plannedList: PlannedList)
}

接口(interface)的实现:

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

据我所知,如果我想获得一个接口(interface),我可以使用@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),因为您是 View 模型中的注入(inject)存储库

关于android - Dagger 柄 : Repository cannot be provided without an @Provides-annotated method,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70975323/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com