Use a single triggering mechanism to update your composable. Your list should just be available as a normal variable. You can add/remove/delete items in the list and even update properties of each item. Once you make these updates to the list, you can recompose your composable by generating a random number that is bound to a mutable state observable that is observed in your composable:
Kotlin:
class MyViewModel: ViewModel() {
var childTravellersList = mutableListOf<TravellersDetails>()
var onUpdate = mutableStateOf(0)
private fun updateUI() {
onUpdate.value = (0..1_000_000).random()
}
fun update(index){
childTravellersList[index].error = true
updateUI()
}
}
@Composable
fun MyComposableHandler() {
// This will detect any changes to data and recompose your composable.
viewmodel.onUpdate.value
MyComposable(
travelersList = viewmodel.childTravellersList
)
}
@Composable
fun MyComposable(
travelersList: List<TravellersDetails>
) {
}
You should avoid creating multiple mutable state variables in your viewmodel for different variables that need updating. There is absolutely no need to do this. Just create a single mutable state variable, as shown above. Whenever you need to recompose your composable, you just updateUI function. Logic in your viewmodel should decide what needs to be updated but leave the actual updating mechanism to a single mutable state observable. This is a clean pattern and will make updating your composables much easier.
However, do keep in mind that your UI is normally going to be made up of many composables in a hierarchy. You don't want to recompose the entire hierarchy when just one element changes. For that reason, a mutable state observable should be used for each composable that needs to be recomposed independently of the others in the heirarchy.
The other benefit of using the solution shown above is that you can easily update objects without the need to create new objects. If you want your composable to recompose when only a certain property of the object changes, you cannot use a mutable state observable because they only detect changes to the object themselves and NOT to the object's properties. This is why you are better off to use the triggering method shown above and simply retrieve the updated object when the composable recomposes.