我将 Hilt 添加到我的项目中,现在LiveData
返回错误Object type
。也许我在代码中做了一些错误的更改。
getAllCurrencies
返回LiveData<Resource<Unit>>
,但它应该LiveData<Resource<Currencies>>
视图模型:
class SplashScreenViewModel @ViewModelInject constructor(
private val roomDatabaseRepository: RoomDatabaseRepository,
private val retrofitRepository: RetrofitRepository) : ViewModel() {
fun getAllCurrencies(mainCurrency: String) = liveData(Dispatchers.IO) {
emit(Resource.loading(data = null))
try {
emit(Resource.success(data = retrofitRepository.getAllCurrencies(mainCurrency)))
} catch (exception: Exception) {
emit(Resource.error(data = null, message = exception.message ?: "Error Occurred!"))
}
}
存储库:(它返回好的类型)
class RetrofitRepository @Inject constructor(val currenciesApiHelper: CurrenciesApiHelper) {
suspend fun getAllCurrencies(mainCurrency: String) {
currenciesApiHelper.getAllCurrencies(mainCurrency)
}