I have a list used in a `LazyColumn` and a corresp...
# compose-desktop
s
I have a list used in a
LazyColumn
and a corresponding add function in the viewmodel but no matter what I try the list does not refresh itself. basically the same code is used for a search feature which works fine. only difference it's not called from a
onClick
but from
onValueChanged
Screen:
Copy code
val decks by viewModel.decks.collectAsState()
ViewModel
Copy code
private val _decks = MutableStateFlow(Client.userDecks.value)
    val decks: StateFlow<List<Deck>> = _decks

    suspend fun addDeck(deck: NewDeck) {
        Client.addDeck(deck)
        _decks.value = Client.userDecks.value
    }

    suspend fun filterDecks(input: String) {
        _decks.value = Client.userDecks.value.filter { bla bla }
    }
also checked with printing
_decks.value
and it does contain the added value. forcing the list to recompose by switching screens etc also shows the new value
a
What is
Client.userDecks
?
s
StateFlow<List<Deck>>
, Deck being a custom data class
a
What does
Client.addDeck()
do?
s
POST api call and then adding given deck to userDecks as well
well, actually to the
MutableStateFlow
of it. just checked
Copy code
private val myDecks = MutableStateFlow<MutableList<Deck>>(mutableListOf())
val userDecks: StateFlow<List<Deck>> = myDecks
s
I will check it tomorrow, seems promising ty
👍 1
I'm using the StateFlow inside Client for a superordinate side menu that's supposed to always show, hence my rather scuffed code there. we'll see
m
Probably not directly related to your problem but this line in your ViewModel
Copy code
val decks: StateFlow<List<Deck>> = _decks
should more correctly be
Copy code
val decks: StateFlow<List<Deck>> = _decks.asStateFlow()
🙏 1
s
So the issue was indeed the mutable, ty again. (also makes totally sense) but shouldn't
asStateFlow()
be the same as writing the type explicitly?
m
asStateFlow blocks even casts from working. If you just write the type explicitly then it can be casted to a MutableStateFlow again.
👌 1
🙏 1