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

Compose地图UI未随ViewModel的mutableState更新问题排查

问题根源

核心问题是每次Compose重组时都会创建新的LocationViewModel实例,导致:

  • 按钮点击调用的是某一个ViewModel实例的getRestaurantList,该实例的restaurantListResponse确实更新了,但当前SearchScreen和ComposeMapDemo使用的是全新的ViewModel实例(其restaurantListResponse始终为空)。
  • 按钮内日志输出空列表,因为此时的viewModel是重组后新建的实例,和之前调用getRestaurantList的不是同一个。
解决方案

1. 正确创建ViewModel实例

在Compose中不能直接通过LocationViewModel(context)创建ViewModel,这会导致每次重组都生成新实例。必须使用Jetpack Compose提供的viewModel()函数,确保整个Screen生命周期内只有一个ViewModel实例。

修改SearchScreen中的ViewModel创建代码:

@Composable
fun SearchScreen(
    myName: String,
    navController: NavController,
    context: Context = LocalContext.current,
) {
    // 替换原来的直接实例化方式
    val viewModel: LocationViewModel = viewModel {
        LocationViewModel(context)
    }
    var user: User = User(myName, 1, 0.0, 0.0)
    ...
}

2. 确保依赖ViewModel-Compose库

如果项目未引入lifecycle-viewmodel-compose依赖,需要在build.gradle.kts(或build.gradle)中添加:

// 版本可根据官方文档调整为最新
implementation("androidx.lifecycle:lifecycle-viewmodel-compose:2.6.2")

3. 验证逻辑

修改后点击按钮:

  • 按钮内的Log.i会输出ViewModel中最新的restaurantListResponse
  • ComposeMapDemo会因restaurantList的变化触发重组,更新地图上的Marker
额外说明

你的LocationViewModel中用mutableStateOf管理restaurantListResponse的写法是正确的,只要ViewModel实例唯一,状态变化就能正常触发Compose重组。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.25 12:32:39