[compass_app] UI state never updates for the HomeScreen (regression after the implementation of #2788)

未关闭
#2,877 1 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

@Ersaoktaviannn 已经在做这个了。

开始于 2026年8月13日。

  • #2880 来自 @Ersaoktaviannn —— 未关闭

评估

难度
3/5
预计耗时
1-2 天
新手友好度
55/100
Issue 类型
缺陷
描述清晰度
基本清楚
活跃度
冷清
技术栈
dart
领域
desktop-dev

调研方向

首先跟踪 HomeScreenContainer 中的 HomeViewModel 初始化过程,以及通过 go_router 实现的 HomeScreen 路由行为,并将其与 #2788 之前的重建行为进行比较。通过预订一次行程并返回主页来复现 Windows 流程;完成的标准是新行程无需 hot reload 即可显示,并且相关的状态更新已被覆盖或验证。

由索引模型根据 Issue 内容生成。

描述

Serious bug, application doesn't work the inteded way.

(Flutter 3.44.8; platform: Windows; tested on commit 978919b)

Steps to reproduce:

  1. Book a new trip;
  2. Come back to a home screen;
  3. You can't see new trip, unless you hot reload.

Before moving initialization of HomeViewModel into HomeScreenContainer, it relied on rebuild that go_router did:

  1. User moves to the home route '/' or deeper;
  2. All previous routes including the one where user moved are getting rebuilt, which results in creating new instances of viewModels, which results in them calling _load and fetching latest data from repository.

Now it seems that HomeViewModel is just initialized once in HomeScreenContainer and never updates.


Generally according to this statement, the current way go_router "manages" ui states, is wrong, which already was unfolded in issue#2574, but outside of users sharing their ways on how to avoid this behavior, it never really recieved much attention.

So maybe a solution to this bug will also lead to a solution for issue#2574?

I myself temporarly solved issue#2574 in a way where viewModel only updates when user moves directly to a route it was going to. Hopefully this helps:

class RouteLifecycleListener extends StatefulWidget {
  const RouteLifecycleListener({
    required this.onResume,
    required this.child,
    super.key,
  });
  final VoidCallback onResume;
  final Widget child;

  @override
  State<RouteLifecycleListener> createState() => _RouteLifecycleListenerState();
}

class _RouteLifecycleListenerState extends State<RouteLifecycleListener>
    with RouteAware {
  @override
  void didChangeDependencies() {
    super.didChangeDependencies();
    routeObserver.subscribe(this, ModalRoute.of(context)! as PageRoute);
  }

  @override
  void didPopNext() {
    widget.onResume();
  }

  @override
  void dispose() {
    routeObserver.unsubscribe(this);
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return widget.child;
  }
}
...
    GoRoute(
      path: '/home',
      name: AppRoute.home.name,
      builder: (context, state) => Provider(
        create: (context) {
          return HomeViewModel(
            ...
          );
        },
        builder: (context, _) {
          return RouteLifecycleListener(
            onResume: () => context.read<HomeViewModel>().load.execute(),
            child: HomeScreen(viewModel: context.read()),
          );
        },
      ),
      routes: [ ...
主要语言
Dart
星标
19.3k
派生
7.8k
平均合并
1 天 18 小时
30 天内合并 PR
4

环境准备

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

flutter/samples 的其他 Issue

查看 flutter/samples 的全部 Issue

相似的 Issue

更多 Dart Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。