I’m trying to lazy-load items to be displayed in a...
# compose
m
I’m trying to lazy-load items to be displayed in a
Dialog
. To achieve this, I have a state of
MutableList
which is initially empty and then populated via a
LaunchedEffect
. Even though the state is successfully updated, the Dialog does not show the items. Is this something to do with limitations in updating Dialog’s contents, or am I doing something fundamentally wrong?
I’ve found a way to make it work by doing this:
Copy code
var items by remember {
    mutableStateOf<List<Item>>(emptyList())
}
LaunchedEffect(true) {
    items = calculateItems()
}
if (items.isNotEmpty()) {
    MyDialog(items, onDismissRequest)
}
although I’m not sure why it wasn’t also working when the
LaunchedEffect
was specified within the
MyDialog
composable.
You could consider `produceState`for this, which is basically just a wrapper around what you’re doing, but might be easier to understand
Copy code
val items = produceState(emptyList()){
    value = calculateItems()
}
m
Thanks Mini, that’s better!