错误:[Dagger/MissingBinding]。如果没有 @Provides 注释的方法,则无法提供

问题描述 投票:0回答:1

我尝试使用 Hilt 将天气存储库注入到我的视图模型中,但收到以下错误消息:

如果没有 @Provides 注释的方法,则无法提供 WeatherDao。
公共抽象静态类 SingletonC 实现 WeatherApplication_GenerateInjector

天气 DAO:

@Dao
interface WeatherDao {
    @Insert(onConflict = OnConflictStrategy.REPLACE)
    suspend fun insertWeatherItem(weatherItem: WeatherItem)
    @Query("DELETE FROM WEATHERITEM WHERE cityName = :cityName")
    suspend fun deleteWeatherItem(cityName: String)
    @Query("SELECT * FROM WEATHERITEM WHERE cityId = :id")
    fun getWeatherItem(id: Int): Flow<WeatherItem>
    @Query("SELECT * FROM WEATHERITEM")
    fun getAllWeatherItems(): Flow<List<WeatherItem>>
}

天气知识库:

class WeatherRepository @Inject constructor(private val weatherDao: WeatherDao) {
     suspend fun insertWeatherItem(weatherItem: WeatherItem) = weatherDao.insertWeatherItem(weatherItem)
     suspend fun deleteWeatherItem(cityName: String) = weatherDao.deleteWeatherItem(cityName)
     fun getWeatherItem(id: Int): Flow<WeatherItem> = weatherDao.getWeatherItem(id)
     fun getAllWeatherItems(): Flow<List<WeatherItem>> = weatherDao.getAllWeatherItems()
}

天气数据库:

@Database(entities = [WeatherItem::class] ,version = 1)
abstract class WeatherDatabase :RoomDatabase(){
    abstract fun weatherDao(): WeatherDao
}

天气模块:

@Module
@InstallIn(ActivityComponent::class)
object WeatherModule {
    @Provides
    @Singleton
    fun provideDatabase(@ApplicationContext context: Context): WeatherDatabase {
        return Room.databaseBuilder(
            context,
            WeatherDatabase::class.java,
            "WeatherDatabase")
            .fallbackToDestructiveMigration()
            .build()
    }
    @Provides
    @Singleton
    fun provideUserDao(weatherDatabase: WeatherDatabase): WeatherDao {
        return weatherDatabase.weatherDao()
    }

    @Provides
    @Singleton
    fun provideWeatherRepository(weatherDao: WeatherDao): WeatherRepository {
        return WeatherRepository(weatherDao)
    }
}

天气视图模型:

@HiltViewModel
class WeatherViewModel @Inject constructor(
    private val weatherRepository: WeatherRepository,
) : ViewModel()

主要活动:

@AndroidEntryPoint
class MainActivity : ComponentActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        val viewModel: WeatherViewModel by viewModels()

        setContent {
           
            WindSpellTheme(darkTheme = darkTheme) {
                Surface(
                    modifier = Modifier.fillMaxSize(),
                    color = MaterialTheme.colorScheme.background
                ) {
                    MainScreen(viewModel) {
                        
                    }
                }
            }
        }
    }
}

应用:

@HiltAndroidApp
class WeatherApplication: Application()

Hilt版本是2.47

android kotlin android-room dagger dagger-hilt
1个回答
0
投票

请尝试从

@Inject constructor
中删除
class WeatherRepository @Inject constructor(private val weatherDao: WeatherDao)
,因为您已经在
WeatherModule
中拥有它。

目前应该可以工作。

P.S. 如果您稍后决定制作

WeatherRepository
及其实现的接口,例如
WeatherRepositoryImpl
,您可以按如下方式进行:

@Singleton
class WeatherRepositoryImpl @Inject constructor(private val weatherDao: WeatherDao): WeatherRepository {
    //implementation
}

然后,在某些

Module
中,这是一个接口抽象类

@Binds
@Singleton
fun bindWeatherRepository(impl: WeatherRepositoryImpl): WeatherRepository
© www.soinside.com 2019 - 2024. All rights reserved.