我有一个MutableStateFlow
,差不多是emptyList
.当我添加项目时,我的视图被重新组合,没有任何问题.现在,当列表被清除时,我想重新组合视图.我try 了一些代码,但什么也没有发生.
PairViewModel个
class PairViewModel : BaseViewModel() {
val scanLowEnergyDevices by lazy { MutableStateFlow(emptyList<ScanResult>()) }
fun addDevices(result: ScanResult) {
scanLowEnergyDevices.value += result
}
}
ContentStateful个
@Composable
fun ContentStateful(
context: Context = LocalContext.current,
viewModel: BloodPressurePairViewModel = getViewModel()
) {
val activity = context as ComponentActivity
val scanDeviceList by viewModel.scanLowEnergyDevices.collectAsStateWithLifecycle()
ContentStateLess(
scanDeviceList = scanDeviceList,
resetAction = {
viewModel.scanLowEnergyDevices.value.toMutableList().clear()
}
)
}
ContentStateLess个
@Composable
fun ContentStateLess(
scanDeviceList: List<ScanResult>,
resetAction: () -> Unit,
) {
AnimatedVisibility(visible = scanDeviceList.isNotEmpty()) {
Text(text = "scanDeviceList ${scanDeviceList.size}")
Button(onClick = { resetAction() }) {
Text(text = "Reset")
}
}
}
这里出了什么问题?谢谢