@Composable
fun CategorySection(context: Context) {
Column(modifier = Modifier.padding(8.dp)) {
Text(
text = "Api Data",
style = MaterialTheme.typography.h5,
modifier = Modifier.padding(vertical = 7.dp, horizontal = 8.dp)
)
val coroutineScope = rememberCoroutineScope()
coroutineScope.launch(Dispatchers.Main) {
val response = ApiPhoto.apiService.getPhotos()
if (response.isSuccessful && response.body() != null) {
val photos = response.body()
photos?.let {
CategoryItemSection(it)
}
} else {
Toast.makeText(
context,
"Error Occurred: ${response.message()}",
Toast.LENGTH_LONG
).show()
}
}
}
}
@Composable
fun CategoryItemSection(photos: List<Photo>) {
LazyColumn(
modifier = Modifier.fillMaxWidth()
){
items(photos.size){
CardView(photos[it])
}
}
}发布于 2021-11-02 05:32:17
您不能从协程内部调用CategoryItemSection。你在这里的编码实践是完全错误的。您不应该从可组合组件中对数据源进行API调用。使用视图模型和状态提升:
阅读状态提升和单向数据流:
https://stackoverflow.com/questions/69803611
复制相似问题