mutableStateMapOf

Function

Common
@StateFactoryMarker
public fun <K, V> mutableStateMapOf(): SnapshotStateMap<K, V>

Create a instance of MutableMap<K, V> that is observable and can be snapshot.

Common
@StateFactoryMarker
public fun <K, V> mutableStateMapOf(vararg pairs: Pair<K, V>): SnapshotStateMap<K, V>

Create a instance of MutableMap<K, V> that is observable and can be snapshot.

Code Examples

stateMapSample

fun stateMapSample() {
    @Composable
    fun NamesAndAges() {
        var name by remember { mutableStateOf("name") }
        var saying by remember { mutableStateOf("saying") }
        val sayings = remember {
            mutableStateMapOf(
                "Caesar" to "Et tu, Brute?",
                "Hamlet" to "To be or not to be",
                "Richard III" to "My kingdom for a horse",
            )
        }
        Column {
            Row {
                BasicTextField(value = name, onValueChange = { name = it })
                BasicTextField(value = saying, onValueChange = { saying = it })
                Button(onClick = { sayings[name] = saying }) { Text("Add") }
                Button(onClick = { sayings.remove(name) }) { Text("Remove") }
            }
            Text("Sayings:")
            Column {
                for (entry in sayings) {
                    Text("${entry.key} says '${entry.value}'")
                }
            }
        }
    }
}