I have a file list, need query data from each file, then render Result list to LazyColumn
But I dont want render list until all the query finished, like this:
class Repo {
private val list = listOf(fileA, fileB)
fun query(key: String): Flow<List<Result>>
= list.asFlow().map{ it.query(key) }.toList()
}
@Composable
fun ListView(repo: Repo){
val res = repo.query("xxx").collectAsState(xxx)
LazyColumn{
items(res){ xxx }
}
}
How can I render a LazyColumn from Flow.collectAsState progressive
class Repo {
private val list = listOf(fileA, fileB)
fun query(key: String): Flow<Result>
= list.asFlow().map{ it.query(key) }
}
@Composable
fun ListView(repo: Repo){
val res = repo.query("xxx").collectAsState(xxx)
LazyColumn{
// ???
items(res){ xxx }
}
}
Just found derivedStateOf function work perfect for this.
class Repo {
private val list = listOf(fileA,fileB)
fun query(key: String): Flow<Result> = list.asFlow().map{xxx}
}
@Composable
fun ListView(repo: Repo){
val flow = repo.query("xxx").collectAsState(xxx)
val update by remember {
val data = mutableListOf<Result>()
derivedStateOf {
flow?.run(data::add)
data
}
}
LazyColumn {
items(update) { data: Result ->
xxxx
}
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With