lava
lava

Reputation: 7343

cannot be provided without an @Provides-annotated method. hilt

Log

C:\Users\hasif\AndroidStudioProjects\SamplePhone\app\build\generated\hilt\component_sources\debug\com\sherhotel\samplephone\MyApplication_HiltComponents.java:127: error: [Dagger/MissingBinding] com.sherhotel.samplephone.mhilt.AppDatabase cannot be provided without an @Provides-annotated method.
      public abstract static class SingletonC implements MyApplication_GeneratedInjector,
                             ^
          com.sherhotel.samplephone.mhilt.AppDatabase is injected at
              com.sherhotel.samplephone.mhilt.MyModule.provideDocumentDao(db)
          com.sherhotel.samplephone.mhilt.DocumentDao is injected at
              com.sherhotel.samplephone.mhilt.Repository(dao)
          com.sherhotel.samplephone.mhilt.Repository is injected at
              com.sherhotel.samplephone.mhilt.MyViewModel(db)
          com.sherhotel.samplephone.mhilt.MyViewModel is injected at
              com.sherhotel.samplephone.mhilt.MyViewModel_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.sherhotel.samplephone.MyApplication_HiltComponents.SingletonC ? com.sherhotel.samplephone.MyApplication_HiltComponents.ActivityRetainedC ? com.sherhotel.samplephone.MyApplication_HiltComponents.ViewModelC]

gradle dep

 // Dagger-Hilt
    implementation "com.google.dagger:hilt-android:2.43.2"
    kapt "com.google.dagger:hilt-compiler:2.43.2"
    implementation 'androidx.hilt:hilt-navigation-compose:1.0.0'
    kapt 'androidx.hilt:hilt-compiler:1.0.0'
    // Room
    kapt "androidx.room:room-compiler:$room_version"
    implementation "androidx.room:room-runtime:$room_version"
    implementation "androidx.room:room-ktx:$room_version"
    // ViewModel Compose
    implementation "androidx.lifecycle:lifecycle-viewmodel-compose:2.5.1"

Code

    @Module
    @InstallIn(SingletonComponent::class)
    object MyModule {
    
        @Provides
        @Singleton
        fun provideDatabase(@ApplicationContext appContext: Context): RoomDatabase {
            return Room.databaseBuilder(
                appContext,
                AppDatabase::class.java,
                "wise_pdf_db"
            ).fallbackToDestructiveMigration()
                .build()
        }
    
        @Provides
        @Singleton
        fun provideDocumentDao(db: AppDatabase): DocumentDao =
            db.documentDao()
    
    
    }
    
    
    @Database(entities = [Document::class], version = 2, exportSchema = false)
    public abstract class AppDatabase : RoomDatabase() {
    
        abstract fun documentDao(): DocumentDao
    }
    
    @Dao
    interface DocumentDao {
    
        @Query("SELECT * FROM my_pdf_docs ORDER BY lastRead DESC")
        fun getRecentDocs(): LiveData<List<Document>>
    
        @Insert(onConflict = OnConflictStrategy.IGNORE)
        suspend fun insert(document: Document)
    
        @Query("DELETE FROM my_pdf_docs WHERE id = :id")
        suspend fun delete(id: Int)
    }
    
    
    @Entity(tableName = "my_pdf_docs", indices = [Index(value = ["filePath"], unique = true)])
    data class Document(
        @PrimaryKey(autoGenerate = true) val id: Int,
        @ColumnInfo(name = "filePath") val filePath: String,
        @ColumnInfo(name = "lastRead") val lastRead: Long
    )

@HiltViewModel
class MyViewModel @Inject constructor(private val db: Repository) : ViewModel() {
//    fun getData(): String {
//        return "123"
//    }
    suspend fun insert(document: Document) {
        db.insert(document)
    }
    val recentDocuments: LiveData<List<Document>> = db.getRecentDocs()
}

class Repository @Inject constructor(private val dao: DocumentDao) {
    suspend fun insert(document: Document) {
        dao.insert(document)
    }

    fun getRecentDocs(): LiveData<List<Document>> {
        return dao.getRecentDocs()
    }
    // class methods here
}

@HiltAndroidApp
class MyApplication : Application()



@AndroidEntryPoint
class MainActivity : ComponentActivity() {
    val viewModel: MyViewModel by viewModels()

what i missing here ?.

Upvotes: 0

Views: 386

Answers (1)

IR42
IR42

Reputation: 9672

Change the return type of fun provideDatabase to AppDatabase

fun provideDatabase(@ApplicationContext appContext: Context): AppDatabase

Upvotes: 1

Related Questions