gpt4 book ai didi

android - 撰写foreach循环:@Composable invocations can only happen from the context of a @Composable function

转载 作者:行者123 更新时间:2023-12-04 23:43:44 30 4
gpt4 key购买 nike

我正在尝试迭代 List对象,对于每个我想要显示可组合卡片的对象。问题是你不能从 list.forEach{} 内部调用 Composable 函数。括号。
编码:

@Composable
fun Greeting(listy : List<SomethingForLater>) {
LazyColumn {
listy.forEach {
//error here
testCard(somethingForLater = it)
}

}
}
@Composable
fun testCard(somethingForLater: SomethingForLater){
val theme = MaterialTheme
Card(shape = theme.shapes.small,backgroundColor = theme.colors.secondary){
Column {
Row {
Text(
text = somethingForLater.task,
modifier = Modifier.padding(start = 5.dp,
top = 3.dp,bottom = 3.dp
),
fontSize = 18.sp,
fontWeight = FontWeight.Bold,

)
}
}
}
}

最佳答案

items LazyColumn 中的参数

LazyColumn {
items(listy) { message ->
testCard(message)
}
}
或者您可以简单地更改 LazyColumnColumn

关于android - 撰写foreach循环:@Composable invocations can only happen from the context of a @Composable function,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67961596/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com