2

As below code showed, I got the list(type: Flow<List<T>>),

how can I use it to update lazyColumn's item, so when the room data changed, lazyColumn updates items accordingly?

Many thanks.

@Composable
fun SomeContent(context: Context) {
    // get the view model ref
    val viewModel: SomeViewModel =
        viewModel(factory = SomeViewModelFactory(Db.getInstance(context)))

    // get the list from room, it is a Flow list
    val list = viewModel.sumDao.getAllRows()

    LazyColumn(
    ) {
        // I need to use the list in below, but got errors, how to do?
        items(list.size) {
        SomeListItem(list[it])
        }
    }
}
caibirdcnb
  • 275
  • 5
  • 18

2 Answers2

2

You must collect your FlowList as follows

val list = viewModel.sumDao.getAllRows().collectAsState(initial = emptyList())

Instead of items, use itemsIndexed

itemsIndexed(list) {idx, row -> SomeListItem(row)}

Let me know if the above works.

vn1gam
  • 128
  • 1
  • 8
0

if using vn1gam's solution, might need to do

itemsIndexed(list.value) {idx, row -> SomeListItem(row)}
  • 1
    This does not provide an answer to the question. Once you have sufficient [reputation](https://stackoverflow.com/help/whats-reputation) you will be able to [comment on any post](https://stackoverflow.com/help/privileges/comment); instead, [provide answers that don't require clarification from the asker](https://meta.stackexchange.com/questions/214173/why-do-i-need-50-reputation-to-comment-what-can-i-do-instead). - [From Review](/review/late-answers/31900949) – ldrg Jun 06 '22 at 13:46