You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

MutableLiveData未正常更新状态:加载状态无法切换问题

问题:提交Loading状态后UI状态无法更新

我在从服务器下载数据时,尝试通过postValue提交Loading状态来向用户显示进度条,代码如下:

private fun loadBottomSheetItems(currentViewState: BusinessMapViewState.Display, getBusinessByIdsRequest: GetBusinessByIdsRequest) {
 
    viewModelScope.launch {
        _businessMapViewState.postValue(
            currentViewState.copy(
                bottomSheetState = BottomSheetViewState.Loading <--------------- 始终保持该状态!
            )
        )
 
        val responseFlow = businessRepository.getBusinessListByIds(
            getBusinessByIdsRequest
        )
 
        responseFlow.collect { result ->
            if (result.isSuccess()) {
                val businesses = result.asSuccess().value.businessList
 
                _businessMapViewState.postValue(
                    currentViewState.copy(
                        bottomSheetState = BottomSheetViewState.Display(
                            items = businesses.map { business ->
                                BusinessListCardItemModel(
                                    businessId = business.id,
                                    businessName = business.name
                                )
                            }
                        )
                    )
                )
            } else {
                _businessMapViewState.postValue(
                    currentViewState.copy(
                        bottomSheetState = BottomSheetViewState.Error
                    )
                )
            }
        }
    }
}

提交Loading状态后,数据加载完成该状态却无法改变。如果移除Loading状态的postValue代码块,或添加延迟,数据就能正常显示,但我需要显示进度条。我还尝试将Loading状态的postValue代码块移到viewModelScope外,依旧没有变化。

更新
我已解决问题,是代码的其他部分修改了UI状态 🙈


内容的提问来源于stack exchange,提问作者Ilya Shutov

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.01 19:06:18