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
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.