Why in this simple experiment, the square is not u...
# compose
l
Why in this simple experiment, the square is not updated when it is clicked ? Though I'm pointing to the last element of the list in the Text
l
You should use mutableStateList, or set number.value = number.value + 1
👍🏾 1
MutableList is not integrated with Compose, so Compose can’t know when it is updated. There is a MutableStateList or something along those lines, or you can manually update your number variable.
👍🏾 1
l
Thank you very much : I'm gonna try with mutableStateList 🙂. I'm trying the hard way here, as I have component in my project which does not work well, and I need this kind of update.
It works : it's just that I need to implement the Saver myself if I want to use rememberSaveable. It's not given by default. Thank you very much 🙂
l
If you want to keep rememberSaveable, you can use an immutable list, and create a new list when you update.
Copy code
var numbers by rememberSaveable { mutableStateOf(listOf(3)) }

...

numbers = numbers + 1
Edit: I originally wrote val, not var. This wouldn’t compile in that case
l
Thank you. So I understand better. I need a mutable list. Maybe this can be a smell of a bad design from me.
l
Compose doesn’t understand MutableList. You’d either need a MutableStateList, or use a List and set the list variable.
l
So can I try with
mutableStateOf{ myMutableList }
?
l
You could, but I wouldn’t use a mutable list in that case. If you mutate the list, Compose won’t get notified. You’d need to set your state variable, which means the list can be immutable.
Like I did in the example a few messages back.
👍🏾 1
l
Ok, so I managed build "appending" items to an immutable list.
l
That looks right.
👍🏾 1
l
Thank you very much for those clarifications 🙂
l
I don’t know without a compiler if the ‘by’ syntax works when you wrap the mutableStateOf with rememberSaveable, but it should.
The point of using ‘by’ instead of ‘=’ is just so you don’t have to write ‘.value’ everywhere.
l
Yes, it does. I knew that feature 🙂
I've complicated the experiment a bit : • I'm using a child component for the text • I'm using an extension function for incrementing the value : it simply add a new value incremented from the last element. But this time, clicking on the text does not update the value any more, why ? This code is closer from a code I've written for a complex composable in my project : hence the complication.
l
You need to say ‘number = number.advance()’
👍🏾 1
Compose is looking for ‘number = …’ as a general rule, it won’t recompose unless it sees this.
🆗 1
l
Thank you it works again 🙂 I'm trying to report the change in my main composable of my project 🙂
l
I find it easiest to use StateFlow and collectAsState(), since any time I call myStateFlow.update { doSomething(it) }, recomposition will be triggered.
But that’s more for when you set up some sort of separate state management.
l
Yes, you're right. I've seen there are some tutorials about StateFlow (one is the advance codelabs about state). So I think I should also learn a bit more about them and their usage 🙂
👍 1