Lilly
09/02/2021, 9:49 PMFlow that comes from view model? I have a Flow that produces values for LazyColumn. Do I have to put the values into a SnapshotStateList or can I feed the LazyColumn on the fly with the values? How would I do this?Zach Klippenstein (he/him) [MOD]
09/02/2021, 10:19 PMcollectAsState?Zach Klippenstein (he/him) [MOD]
09/02/2021, 10:35 PMLilly
09/02/2021, 10:37 PMOh, the flow produces individual values, and you want to aggregate them into a list?Yes if I have to aggregate them.
Zach Klippenstein (he/him) [MOD]
09/02/2021, 10:37 PMSnapshotStateList you suggested could work, but you could also use the flow scan operator to create the list on the flow side.Zach Klippenstein (he/him) [MOD]
09/02/2021, 10:38 PMval allValues by remember(values) {
values.scan(emptyList()) { list, value -> list + value }
}.collectAsState(emptyList())
where values is your original flowZach Klippenstein (he/him) [MOD]
09/02/2021, 10:39 PMval allValues by remember { mutableStateListOf() }
LaunchedEffect(values) {
values.collect { allValues += it }
}Lilly
09/02/2021, 10:40 PMZach Klippenstein (he/him) [MOD]
09/02/2021, 10:42 PMLilly
09/02/2021, 10:43 PMval allValues by remember { mutableStateListOf() }
LaunchedEffect(values) {
values.collect { allValues += it }
}
Taking this example. Where is the different, when collecting the flow in the vm and store the items in a val items = mutableStatelistOf() . From UI, I could use items directly.Zach Klippenstein (he/him) [MOD]
09/02/2021, 10:44 PMZach Klippenstein (he/him) [MOD]
09/02/2021, 10:45 PMmutableStateListOf in your vm, that's fine, but you'd need to manage the coroutine that did the collection somehow (maybe .launchIn(viewModelScope)?)Lilly
09/02/2021, 10:50 PMSo if you did theWould be a great candidate forin your vm, that's fine, but you'd need to manage the coroutine that did the collection somehow (maybemutableStateListOf?).launchIn(viewModelScope)
init blockZach Klippenstein (he/him) [MOD]
09/02/2021, 10:59 PMZach Klippenstein (he/him) [MOD]
09/02/2021, 10:59 PMlhwdev
09/02/2021, 11:01 PMZach Klippenstein (he/him) [MOD]
09/02/2021, 11:31 PMLilly
09/03/2021, 12:08 AM