0

我正在传递数据以将列表呈现为Flow<>. 如果它是空的,而不是列表,我需要显示题词“无数据”。如何设置条件来检查流是否为空?

@Composable
fun HistoryLayout(viewModel: HistoryViewModel = viewModel()) {
    val historyFlow = viewModel.historyStateFlow.collectAsLifecycleState().value
    if (is historyFlow empty???) {
        Box(
            modifier = Modifier.fillMaxSize(),
            contentAlignment = Alignment.Center
        ) {
            Text(
                stringResource(R.string.emptylist),
                textAlign = TextAlign.Center,
                maxLines = MAX_LINES,
                overflow = TextOverflow.Ellipsis,
            )
        }

    } else {
        HistoryTableList(historyFlow = historyFlow)
    }
}
4

1 回答 1

0

这个技巧帮助我解决了问题,但是我希望仍然找到更好的解决方案 From Flowi get LazyPagingItems<...>,它有*.itemCount方法。

@Composable
fun HistoryLayout(viewModel: HistoryViewModel = viewModel()) {
    val historyFlow = viewModel.historyStateFlow.collectAsLifecycleState().value
    val historyItems: LazyPagingItems<HistoryRecordEntity> = historyFlow.collectAsLazyPagingItems()

    if (historyItems.itemCount == 0) {
        Box(
            modifier = Modifier.fillMaxSize(),
            contentAlignment = Alignment.Center
        ) {
            Text(
                stringResource(R.string.emptylist),
                textAlign = TextAlign.Center,
                maxLines = MAX_LINES,
                overflow = TextOverflow.Ellipsis,
            )
        }
    } else {
        HistoryTableList()
    }
}
于 2022-01-25T11:23:11.957 回答