Lazycolumn内部的Lazycolumn
我正在尝试使用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
I am trying to create and list with sub list using LazyColumn
with the code below
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")
}
}
}
}
Error:
@Composable invocations can only happen from the context of a @Composable function
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
让我尝试解释一下您应该改变什么。
1。为什么发生错误?
如果我们窥视
lazycolumn
代码,我们可以找到content> content:lazylistscope。() - &gt ;单位
作为内容参数数据类型。这表明上下文不具有可理解的上下文。
相反,
column
/行之类的复合物将具有
。content:@composable columnScope。() - >单位
/<代码>内容:@composable rowscope。() - &gt;单位@composable
显示content
具有合并的上下文。2。如何修复?
从我在代码中看到的内容,您不需要
lazycolumn
在另一个lazycolumn
中。您将需要一个LazyColumn
,其中有多个项目来自不同的数据源。您可以像这样更改代码,
3。
item
vs项目
使用
项目
而不是itegs(1)
如果您有一个物品,因为它们是等效的,但这会更加清楚。ps:
lazycolumn
使用项目
items 具有itemcontent
带有可合并的上下文。因此,我们可以在其中添加组合。Let me try to explain in parts what you should be changing.
1. Why did the error occur?
If we peek into
LazyColumn
code, we can findcontent: LazyListScope.() -> Unit
as the content parameter datatype.This shows that the context does not have composable context.
On contrary, composables like
Column
/Row
would havecontent: @Composable ColumnScope.() -> Unit
/content: @Composable RowScope.() -> Unit
respectively.The
@Composable
shows that thecontent
has a Composable context.2. How to fix it?
From what I see in the code, you don't need a
LazyColumn
inside anotherLazyColumn
. You would need oneLazyColumn
with multiple items in it from different data sources.You can change your code like this,
3.
item
vsitems
Use
item
instead ofitems(1)
if you have a single item as they are equivalent, but this would be more clear.P.S:
LazyColumn
usesitem
oritems
which haveitemContent
with composable context. Hence we can add Composables inside them.