嗨,我是不熟悉jetpack compose的人,在下面的代码中,我试图从http请求(电影列表)中获取数据,然后将每部电影添加到LazyColumnFor列表(就像recyclerView)>
但是,我传递给LazyColumn的列表不兼容,并且出现以下错误:
lazyColumnFor Required:List<TypeVariable(T)>Found:List<MovieItem>?
下面是我的可组合函数
@Composable
fun getTopMovies(){
val topMovies by movieListViewModel.getTopMovies().observeAsState()
LazyColumnFor(items = topMovies?.data?.results?)
}
我使用globalScope库鲁汀进行API调用
val result = MutableLiveData<Resource<DATA>>()
result.value = Resource.loading(null)
GlobalScope.launch {
try {
val response = client.await()
if (response.isSuccessful) {
val data = response.body() as DATA
result.postValue(Resource.success(data))
}
} catch (e: Exception) {
result.postValue(Resource.error(e.toString(), null))
}
我返回的电影列表如下:
data class MovieList(val page: Int,
@SerializedName("total_results") val totalResults: Int,
@SerializedName("total_pages") val totalPages: Int,
val results: List<MovieItem>)