
In this example, the only "correct" place to mutate is the value of the MutableState, since that's the only one that will actually cause change notifications to be sent.


This is bad for multiple reasons: it's not clear to readers of the code how they should change the value, and it means it's not safe to pass any of the nested values around because some other code might decide to just re-assign data. By changing the value of the MutableState: data.value = otherList.By re-assigning data to point to a different MutableState: data = otherMutableState.If you found this in a codebase, and you had to update this value, how would you do it? You've got three options: If you really want to use one of these but need a Set, you can just use a SnapshotStateMap with a value type of Unit.Įnter fullscreen mode Exit fullscreen mode Compose provides two collection types for this: mutableStateListOf(): SnapshotStateList and mutableStateMapOf(): SnapshotStateMap. This only works if you're using Compose's state system, because RxJava, LiveData, Flow, etc. Use a special collection type that can send its own change notifications when you mutate it.The easiest way to do this is to use the standard Kotlin read-only collections, like listOf(), and the standard operators on those collections, like plus. Store an immutable collection in the state holder, and update the collection by creating a new copy.Because the old and new state objects are actually the same object, they will always be considered equal (this is part of the contract of the equals method in Kotlin and Java). They will not send change notifications if you mutate the object they're holding, then set that same object as the new state.They will not send change notifications if you simply change some properties of the object they're holding.State holders like MutableStateFlow, and MutableState only send change notifications when the old and new state values are not equal. If someone sent you a link to this post as a reply to a question on Slack or somewhere, and you just want a quick answer, here you go:ĭon't put mutable collections inside mutable state holders. It also focuses on collections, but applies to any data structures you're using to hold state. This post is largely applicable to any reactive programming library, although it does focus a little extra on Compose in spots. This post isn’t going to try to cover all the reasons why that might be, but just the most common one, which I’ve seen come up at least a couple times a week.

Unfortunately, sometimes the UI doesn’t update when you change the data. We’ve all done it: put some data in a list, changed the data around a little, and rendered the list in some UI.
