You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Hilt报错:无@Provides注解致InterfaceGetProductDetailsRepository无法注入

解决InterfaceGetProductDetailsRepository无法被Dagger Hilt提供的问题

这个报错的核心原因是Dagger Hilt找不到如何创建InterfaceGetProductDetailsRepository接口的实例——虽然你用MockRepository作为该接口的实现类,但Hilt不会自动将实现类与接口绑定,必须显式声明绑定关系。

解决步骤:

  1. 在DataModule中添加@Provides方法,将MockRepository绑定到InterfaceGetProductDetailsRepository接口:
@Module
@InstallIn(SingletonComponent::class)
class DataModule {
    // 保留原有的provideMockRepository和provideSaveToken方法
    
    @Provides
    @Singleton
    fun provideInterfaceGetProductDetailsRepository(mockRepository: MockRepository): InterfaceGetProductDetailsRepository = mockRepository
}
  1. 确认MockRepository确实实现了InterfaceGetProductDetailsRepository接口:
    确保MockRepository类定义包含该接口实现,示例如下:
class MockRepository(private val tokenStorage: TokenStorage) : InterfaceGetProductDetailsRepository,
    InterfaceGetProductsRepository, // 其他需要实现的接口
    InterfaceGetProfileRepository {
    
    // 实现InterfaceGetProductDetailsRepository的方法
    override suspend fun getDetailsProduct(id: String): Flow<ProductDetail> {
        // 具体业务逻辑
    }
    
    // 其他接口方法的实现...
}

问题根源推测(之前正常现在报错):

  • 之前代码中存在上述绑定方法,但后续被误删除
  • 你修改了GetProductDetailsUseCaseImpl的依赖,从直接依赖MockRepository改为依赖InterfaceGetProductDetailsRepository,但未同步添加接口绑定

完成修改后重新编译项目,Hilt即可正确生成依赖注入代码,解决该报错。

内容的提问来源于stack exchange,提问作者Meridiane

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.22 22:42:49