Lazycolumn内部的Lazycolumn

发布于 2025-01-23 20:16:18 字数 777 浏览 0 评论 0原文

我正在尝试使用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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

英雄似剑 2025-01-30 20:16:18

让我尝试解释一下您应该改变什么。

1。为什么发生错误?

@composable Incocations只能从@composable函数的上下文中发生发生

如果我们窥视lazycolumn代码,我们可以找到content> content:lazylistscope。() - &gt ;单位作为内容参数数据类型。

这表明上下文不具有可理解的上下文。

相反,column/行之类的复合物将具有content:@composable columnScope。() - &gt;单位/<代码>内容:@composable rowscope。() - &gt;单位

@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 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?

@Composable invocations can only happen from the context of a @Composable function occurred

If we peek into LazyColumn code, we can find content: LazyListScope.() -> Unit as the content parameter datatype.

This shows that the context does not have composable context.

On contrary, composables like Column/Row would have content: @Composable ColumnScope.() -> Unit/content: @Composable RowScope.() -> Unit respectively.

The @Composable shows that the content has a Composable context.

2. How to fix it?

From what I see in the code, you don't need a LazyColumn inside another LazyColumn. You would need one LazyColumn with multiple items in it from different data sources.

You can change your code like this,

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 vs items

Use item instead of items(1) if you have a single item as they are equivalent, but this would be more clear.

P.S:
LazyColumn uses item or items which have itemContent with composable context. Hence we can add Composables inside them.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文