I'm using mvvm architecture in my app ,this is my code :
class ProductRepository : BaseRepository() {
private val mutableLiveData = MutableLiveData<ProductsModel>()
fun getProducts(catId: String): MutableLiveData<ProductsModel> {
if (internetAvailable) {
scope.launch {
val request = api.getProducts(catId)
withContext(Dispatchers.Main) {
try {
val response = request.await()
mutableLiveData.value = response
} catch (e: HttpException) {
// Log exception //
Log.v("this", e.message);
} catch (e: Throwable) {
// Log error //)
Log.v("this", e.message);
}
}
}
}
return mutableLiveData
}
}
the repository returns a MutableLiveData by the value of ProductsModel .this is my viewModel class :
class ProductsViewModel :ViewModel(){
val repository=ProductRepository()
private val _products=MutableLiveData<ProductsModel>()
val products:LiveData<ProductsModel>
get()=_products
fun getProducts(catId:String){
_products.value=repository.getProducts(catId)
}
}
I don't understand this error, how can I fix it ? why is it telling me that the type is not true?