Hi guys, I have a mutableStateListOf named descrip...
# compose
t
Hi guys, I have a mutableStateListOf named descriptionListState and I want LaunchedEffect to be triggered when the mutableStateListOf changes. I tried converting mutableStateListOf to list, but it didn't work. Is there any solution?
Copy code
val descriptionListState = remember{ mutableStateListOf("") }

LaunchedEffect(descriptionListState.toList()) {
    alarmViewModel.changeCreateAlarmState(
        createAlarmState.copy(description = descriptionListState.joinToString(" "))
    )
}
r
Never used it, but does this work?
Copy code
val descriptionListState = remember{ mutableStateListOf("") }

LaunchedEffect(Unit) {
    snapshotFlow {
        descriptionListState
    }.collect {
        alarmViewModel.changeCreateAlarmState(
            createAlarmState.copy(description = it.toList().joinToString(" "))
        )
    }
}
Notice that it's reacting to mutable snapshot and whenever it changes then inside
collect
you take the most recent value and convert it to a list
z
You can also just move that list to your ViewModel and keep your logic there. Less spaghetti!
r
Agree
t
Thank you so much Roberto Fuentes. I’ve tried your answer, and it worked.
m
The fact that it works is no indication that it is also a good solution. You should really reconsider @Zun s comment!