如果我得到正确的响应,我已经实现了密封类如果错误 Result.Error,我将得到 Result.Success
在我实现该逻辑的 ViewModel 下方
@HiltViewModel
class GiphyTaskViewModel
@Inject
constructor(private val giphyTaskRepository: GiphyTaskRepository):ViewModel()
{
var giphyresponse=MutableLiveData<Result<List<DataItem>>>()
fun getGifsFromText(apikey:String,text:String,limit:Int)= viewModelScope.launch {
giphyTaskRepository.getGifsFromText(apikey,text,limit).let { response->
if(response?.isSuccessful){
var list=response.body()?.data
giphyresponse.postValue(Success(list))
}else{
giphyresponse.postValue(Error(Exception(response.message())))
}
}
}
}
在我的密封类结果之下
sealed class Result<T>
data class Success(val data: Any) : Result<Any?>()
data class Error(val exception: Exception) : Result<Any?>()
但是当我运行项目时出现以下错误
类型不匹配。必需:结果<列表>!发现:成功