Search code examples
kotlinandroid-jetpack-composedesktop-application

How do I modify the values of mutableStateMapOf?


I've got a field of type mutableStateMapOf:

var dataPool by mutableStateMapOf("archive" to true, "pending" to true)

and I'd like to modify its values via a Checkbox so I tried this:

 Checkbox(checked = true, onCheckedChange = {viewModel.dataPool["archive"]=it})

However, Kotlin shows me that dataPool is a boolean. How comes? I thought I initialized it as map. I don't follow what's happening here. Usually when you initialize one of the mutableStates it has the type I use as a parameter, but why not this time?


Solution

  • It's really strange why it has a delegation that returns a Boolean and throws exception when you try to read it on compose version 1.5.4. And doesn't let you add brackets too.

    You can get SnapshotStateMap<String, Boolean> from mutableStateMapOf with = not by

    val dataPool = remember {
        mutableStateMapOf("archive" to true, "pending" to false)
    }