Does anyone know why after this code my list still...
# compose
r
Does anyone know why after this code my list still filled {0,1,2}
Copy code
var test = mutableStateOf<List<Int>>(emptyList())
/**
 I add some values to test {0,1,2}
**/
test = emptyList()
f
I don't think you can even compile this 😄 If
test
is type
MutableState<List<Int>>
you can't just assign type
List
to it. Should it be
var test by mutableStateOf<List<Int>>(emptyList())
?
Can you please post the whole snippet including the code that adds the values?
r
It is compiling yes😬
Copy code
var filledDays by mutableStateOf<List<Int>>(emptyList())
    var startDay by mutableStateOf<Int?>(null)
    var endDay by mutableStateOf<Int?>(null)
    var nextEndDay: Boolean = false

    fun fill() {
        var list = filledDays.toMutableList()
        for (i in startDay!!..endDay!!) {
            list.add(i)
        }
        filledDays = list
    }

    fun click(day: Int) {
        if (nextEndDay) {
            if (day > startDay!!) {
                endDay = day
                nextEndDay = false
                fill()
            } else {
                startDay(day)
            }
        } else {
            startDay(day)
        }
    }

    fun startDay(day: Int) {
        startDay = day
        endDay = null
        filledDays = emptyList()
        nextEndDay = true
        squareSpeed = 50
    }
f
So I guess this code is not in your UI but in some object. Maybe View model? And your question is why
startDay()
does not clear the
filledDays
by assigning
emptyList()
to it?
r
exactly 🙂
f
Even though I would rather use
mutableStateListOf
instead of
mutableStateOf
I think this should theoretically work. And from where do you observe that
startDay
did not overwrite the state? From UI or you can you see it in the
fill()
function? My question is if the value actually did not change or if the change just did not trigger recomposition. But I haven't touched Compose in few weeks so I might not be the best person to answer this 😅
r
You're right, much better work with
mutableStateListOf
, however I still having the issue. Look at the video, when you click set your starDay and endDay in the calendar, the days which are in the middle are supposed to be added at the same time to the mutableStateListOf and the they should be filled all at the same time, however, when you change your dates quickly, the previous range still filled despite of I remove them from the mutableStateListOf and added again. If you see the last attempt, i'm waiting more time, and my days are filled correctly, all at the same time. When you debug, the mutableStateListOf is successfully empty and filled again, but it seems the previous range still on cache or something if you empty and fill it quickly.