我试图使用LazyColumn和下面的代码创建和列出子列表
DropdownMenu(
expanded = expandedDomain,
onDismissRequest = { expandedDomain = false },
) {
LazyColumn {
items(1) {
Checkbox(checked = false /*checkedState.value*/,
onCheckedChange = {})
Text(text = "$domainResponse.domains[0].name")
}
LazyColumn {
items(domainResponse.domains[0].pwas) { pwas ->
Checkbox(checked = false /*checkedState.value*/,
onCheckedChange = {})
Text(text = "$pwas")
}
}
}
}错误:
@Composable invocations can only happen from the context of a @Composable function发布于 2022-04-25 04:36:31
让我试着解释一下你应该改变什么。
1.为什么会出现错误?
@Composable invocations can only happen from the context of a @Composable function
如果我们查看LazyColumn代码,就可以找到content: LazyListScope.() -> Unit作为内容参数数据类型。
这表明上下文没有可组合的上下文。
相反,像Column/Row这样的可组合材料将分别具有content: @Composable ColumnScope.() -> Unit/content: @Composable RowScope.() -> Unit。
@Composable显示content有一个可组合的上下文。
2.如何解决?
根据我在代码中看到的,您不需要在另一个LazyColumn中使用一个LazyColumn。您需要一个LazyColumn,其中包含来自不同数据源的多个项。
你可以这样修改你的代码,
LazyColumn {
item {
Checkbox(checked = false /*checkedState.value*/,
onCheckedChange = {})
Text(text = "$domainResponse.domains[0].name")
}
items(domainResponse.domains[0].pwas) { pwas ->
Checkbox(checked = false /*checkedState.value*/,
onCheckedChange = {})
Text(text = "$pwas")
}
// You can have multiple lists in a single LazyColumn
items(list2Items) { item ->
Text(text = "$item")
}
}3. item诉items
如果您有一个相同的项目,则使用item而不是items(1),但这将更加清楚。
P.S.:
LazyColumn使用具有可组合上下文的itemContent的item或items。因此,我们可以在其中添加可组合性。
https://stackoverflow.com/questions/71992095
复制相似问题