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

Time:02-04

Hilt points out that this interface cannot be provided without an @Provides annotation:


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

    suspend fun addList(plannedList: PlannedList)

    suspend fun updateList(plannedList: PlannedList)

    suspend fun deleteList(plannedList: PlannedList)
}

Implementation of the interface:

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

As far as I know, if I want to get an interface, I can use @Binds to say to Dagger which implementation should be received. So I did this:

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

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

My ViewModel if it has something to deal with the error:

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

    ...

}

So what should I do to fix the error? The full error message:

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]

Here is the database module:

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

CodePudding user response:

use @InstallIn(ViewModelComponent::class) in your repository module since you are injection repository in your view model

  •  Tags:  
  • Related