Spot the bug
Tapping Add doesn't show the new row. Which line is the bug?
@Composablefun Todo() {val items by remember { mutableStateOf(mutableListOf("Milk")) }Column {items.forEach { Text(it) }Button(onClick = { items.add("Eggs") }) { Text("Add") }}}
Hint 1 of 1
Which writes does Compose observe?
Choose an answer, then open Show answer below to check it.
Show answer
- Answer: line 6
Button(onClick = { items.add("Eggs") }) { Text("Add") }
add mutates the list but doesn't write the State, so Compose isn't told. Use mutableStateListOf("Milk"), or a var holding an immutable List and items = items + "Eggs".