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
LazyColumncode, we can findcontent: LazyListScope.() -> Unitas the content parameter datatype.This shows that the context does not have composable context.
On contrary, composables like
Column/Rowwould havecontent: @Composable ColumnScope.() -> Unit/content: @Composable RowScope.() -> Unitrespectively.The
@Composableshows that thecontenthas a Composable context.2. How to fix it?
From what I see in the code, you don't need a
LazyColumninside anotherLazyColumn. You would need oneLazyColumnwith multiple items in it from different data sources.You can change your code like this,
3.
itemvsitemsUse
iteminstead ofitems(1)if you have a single item as they are equivalent, but this would be more clear.P.S:
LazyColumnusesitemoritemswhich haveitemContentwith composable context. Hence we can add Composables inside them.