Colton Idle
01/15/2021, 10:15 PMAlex Petitjean
01/15/2021, 10:20 PMColton Idle
01/15/2021, 10:20 PMSiyamed
01/15/2021, 10:26 PMColton Idle
01/15/2021, 10:44 PMSiyamed
01/15/2021, 10:47 PMColton Idle
01/15/2021, 10:51 PMSiyamed
01/15/2021, 10:52 PMjim
01/16/2021, 12:22 AMtodoItems.toMutableList().also {
looks very suspicious and probably that codelab needs some additional review.Siyamed
01/16/2021, 12:27 AMjim
01/16/2021, 12:31 AMlistOf<MutableState>
is probably wrong, the user probably wanted mutableStateListOf
instead.
mutableStateOf<List>
could be ok in some circumstances, but it has a very different behavior from mutableStateListOf
Siyamed
01/16/2021, 12:35 AMjim
01/16/2021, 12:48 AMmutableStateListOf<Person>
with each person defined as:
class Person {
var name by mutableStateOf<String>()
}
Siyamed
01/16/2021, 12:51 AMColton Idle
01/16/2021, 1:52 AMjim
01/16/2021, 3:42 AMLazyColumn(...) { index, person ->
TextField(value=person.value, onChange={person.name = it})
}
Colton Idle
01/17/2021, 7:07 PMclass MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
val listOfPeeps = mutableStateListOf<Person>()
listOfPeeps.add(Person())
listOfPeeps.add(Person())
listOfPeeps.add(Person())
setContent {
ComposeTextFieldTheme {
Surface(color = MaterialTheme.colors.background) {
LazyColumn(content = {
item {
Button(onClick = { listOfPeeps.add(Person()) }) {
Text(text = "Add person to list")
}
}
items(listOfPeeps) { person: Person ->
TextField(value = person.name, onValueChange = { person.name = it })
}
})
}
}
}
}
}
class Person {
var name by mutableStateOf<String>("")
}
If you're still watching this thread, one thing I'm still not 100% sure about is why I need
var name by mutableStateOf<String>("")
Couldn't I just have a var name = "" since the list is mutableStateListOf
?jim
01/17/2021, 7:09 PMmutableStateList
only notifies observers when the LIST changes, not when an item within the list is mutated. If you want to be notified when the item within the list changes, that item must be observable (via state).Colton Idle
01/17/2021, 7:13 PMjim
01/17/2021, 7:15 PMColton Idle
01/17/2021, 7:18 PMmutableStateList
only notifies observers when the LIST changes, not when an item within the list is mutated. If you want to be notified when the item within the list changes, that item must be observable (via state)."
So something that is messing with me here is that I've been really hammering home the point that everything should be immutable if it can be. Especially in a kotlin data class like Person. Doing mutableStateOf<String> "feels" wrong. Is my person still following the "rule" of favoring immutablilty when possible?jim
01/18/2021, 10:31 PMdata class Person(val name: String)
And just re-create the person if the name changes. That would be immutable.mutableStateListOf<String>()
, as String is immutable so you don't need to wrap it in a mutableStateOf
and can just remove and add strings as needed (although that gives you less room to grow in the future).mutableState
then you are not favoring immutability.Colton Idle
01/18/2021, 10:38 PMdata class Person(val name: String)
is better, or
class Person {
var name by mutableStateOf<String>()
}
jim
01/18/2021, 10:40 PMColton Idle
01/18/2021, 10:46 PMvar name by mutableStateOf<String>()