簡體   English   中英

沒有 @Provides-annotated 方法就無法提供。 刀柄

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

日志

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 部門

 // 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"

代碼

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

我在這里錯過了什么?

fun provideDatabase的返回類型更改為AppDatabase

fun provideDatabase(@ApplicationContext appContext: Context): AppDatabase

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM