如何检查 Flow 是否为空?

how to check if the Flow is empty?

我正在传递数据以将列表呈现为 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)
    }
}

这个技巧帮我解决了问题,不过我还是希望能找到更好的解决办法 从 Flow 我得到 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()
    }
}