ViewModel 检索空 List 但不应为空 (mutableStateOf)

时间:2021-03-10 07:19:48

标签: android kotlin android-jetpack-compose

我需要一些来自 MainActivity 中的 Viewmodel 的数据才能在另一个可组合中使用它。因此,在获取数据并更新值后,我尝试使用 mutableStateOf() 将其从 viewModel 中取出。

private val locationList: MutableState<MutableList<LocationScaffold>> = mutableStateOf(mutableListOf())

                composable(route = Screen.LocationDestinationListScreen.route) { navBackStackEntry ->
                    val factory = HiltViewModelFactory(LocalContext.current, navBackStackEntry)
                    val viewModel: LocationViewModel =
                        viewModel("LocationDestinationListViewModel", factory)
                    locationList.value = viewModel.locationList.value
                    Log.d("viewModel", "${viewModel.locationList.value}")
                    val lifecycleOwner = LocalLifecycleOwner.current
                    LocationDestinationListScreen(
                        viewModel = viewModel,
                        lifecycleOwner = lifecycleOwner,
                        navigation = { navController.navigate(it) })
                }

                composable(
                    route = Screen.BoxScreen.route + "/{locationId}",
                    arguments = listOf(navArgument("locationId") {
                        type = NavType.IntType
                    })
                ) { navBackStackEntry ->
                    val factory = HiltViewModelFactory(LocalContext.current, navBackStackEntry)
                    val viewModel: HistoryViewModel = viewModel("HistoryViewModel", factory)
                    val activeLocation: Int? = navBackStackEntry.arguments?.getInt("locationId")
                    activeLocation?.let {
                        BoxScreen(viewModel, locationList.value[it])
                    }
                }

这些是导航组合。 LocationDestinationListScreen 开始扫描 BT 设备并从服务器获取一些数据 还有一个 LazyColumn 显示了数据获取的结果,这意味着 viewmodel.locationList.value 绝对不是空的并且会被更新,但不幸的是不是在 MainActivity 端。我在这里遗漏了什么还是忘记了什么?

因为据我所知,流程是:

  1. 构建可组合
  2. 可组合启动中的startScan
  3. 在更新数据的视图模型中发现方法调用 locationList.value
  4. 数据贯穿可组合并应更新所有数据

我觉得这有点奇怪,因为在其他导航上它可以正常工作,例如:

                composable(route = Screen.LoginScreen.route) { navBackStackEntry ->
                    val factory = HiltViewModelFactory(LocalContext.current, navBackStackEntry)
                    val viewModel: LoginViewModel = viewModel("LoginScreenViewModel", factory)
                    when (viewModel.authenticationState.value) {
                        AuthenticationStateListener.Loading -> CenterLoadingIndicator()
                        AuthenticationStateListener.Authenticated -> {
                            navController.navigate(Screen.StartScanScreen.route)
                            if (user.value.name.isNullOrEmpty()) user.value = viewModel.user.value
                        }
                        AuthenticationStateListener.NotAuthenticated -> viewModel.openRequestDialog(
                            this@MainActivity,
                            "Something went wrong",
                            R.drawable.ic_baseline_error_24
                        ) { _, _ -> }
                    }
                    LoginScreen(viewModel = viewModel)
                }

这也会侦听 MutableState 并在状态值更改后正确重定向。

0 个答案:

没有答案