Alex
08/30/2021, 9:59 AMcollectAsState()
(or an equivalent) inside of a LazyListScope
(outside of a @Composable
)?
The usecase is conditionally emitting either A or B items, depending on the content of a StateFlow
val x = derivedStateOf {
controllingStateFlow.value
}
Felix Schütz
08/30/2021, 10:51 AMStateFlow
for each item?Alex
08/30/2021, 10:57 AMFelix Schütz
08/30/2021, 11:03 AMLazyColumn
/ LazyRow
?Alex
08/30/2021, 2:30 PMfun <T> StateFlow<T>.asComposeState(scope: CoroutineScope): State<T> {
val outState = mutableStateOf(value)
scope.launch {
collect {
outState.value = it
}
}
return outState
}
Zach Klippenstein (he/him) [MOD]
08/30/2021, 4:49 PMcollectAsState
though, because this should work:
val decision by decisionFlow.collectAsState()
LazyColumn {
if (decision) {
items(…)
} else {
items(…)
}
}
Alex
08/30/2021, 7:39 PM@Composable
fun Parent() {
LazyColumn {
DecisionSection(viewModel.decisionForm)
}
}
fun LazyListScope.DecisionSection(decisionForm: DecisionForm){
val decision by decisionForm.decision.collectAsState() <-- Can't do, because this is not inside of a composable
if (decision) {
items(…)
} else {
items(…)
}
}
DecisionSection
might be 3 layers down the hierarchy as the above example is extremely simplified. I could hoist decision
but then I would need to pass about 20 parameters from the top level down, which is very cumbersome for lots of reasons.