With this code ```class SomeClass{ val list : ...
# compose
m
With this code
Copy code
class SomeClass{
    val list : SnapshotStateList<Int> = mutableStateListOf(5)
}
val someClass = SomeClass()
snapshotFlow { someClass.list }.onEach { println(it.toString()) }.collectIn(scope)
the onEach block is not executed despite mutating the list I must instead explicitly do
snapshotFlow { someClass.list.toList() }
I am kind of confused why this is the case, and I feel like this must be a pitfall Im likely to run into again. Is this the intended behaviour? Could someone explain why?
a
snapshotFlow
only emits a new value when a value read in the lambda changes. The list instance itself never changes. Its values change, but you are not reading any of its values in the lambda, so the flow won’t emit new values.
toList()
function reads the values of the original list so using that will work.
a
snapshotFlow { someClass.list.toString() }.onEach { println(it) }
is likely to be closer to what you want here, since the resulting string is what changes as part of the snapshot mutations
Don't be afraid to perform that kind of calculation in the snapshotFlow block itself
m
Right, thanks! Printing the list was just an example, I actually need the elements down the flow, but I guess I`ll do toList() then