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

每次进入ListView所在页面内容重复的问题该如何解决?

问题原因

你当前的代码每次触发接口请求拿到经验列表数据后,都会调用addAll()方法往全局存储中已有的experience列表后追加新内容,没有清理上一次进入页面时已经加载的旧数据,因此每次进入页面都会重复添加一遍相同数据,最终导致列表内容重复展示。

解决方案

根据你的业务场景可以选择以下任意一种方案:

  • 方案1:全量更新场景下先清空旧数据再添加新数据
    这是普通全量列表最常用的解决方案,每次拿到新数据前先清空原有列表内容,再添加新拉取的所有数据。修改后的代码如下:
    if (action is GetPersonalExpAction) {
          dataService.getEntity("experience/list").then((reponse) => decodeBody(reponse)).then((result) {
            List<ItemExperience> experience = [];
            
            for (var element in result) {
              experience.add(ItemExperience(
                id: element['id'],
                job: element['job'],
                company: element['company'],
                date_from: element['date_from'],
                date_to: element['date_to'],
                description: element['description'],
              ));
            }
            // 新增代码:先清空原有旧数据
            store.state.personalAreaState.experience!.clear();
            store.state.personalAreaState.experience!.addAll(experience);
            next(action);
          });
        }
    
  • 方案2:增量加载场景下根据唯一ID去重
    如果你需要保留旧数据(比如分页上拉加载更多的场景),可以通过条目唯一ID做判断,只添加列表中不存在的新条目,避免重复:
    if (action is GetPersonalExpAction) {
          dataService.getEntity("experience/list").then((reponse) => decodeBody(reponse)).then((result) {
            List<ItemExperience> experience = [];
            
            for (var element in result) {
              experience.add(ItemExperience(
                id: element['id'],
                job: element['job'],
                company: element['company'],
                date_from: element['date_from'],
                date_to: element['date_to'],
                description: element['description'],
              ));
            }
            // 新增去重逻辑
            final existingIdSet = store.state.personalAreaState.experience?.map((item) => item.id).toSet() ?? {};
            for (var newItem in experience) {
              if (!existingIdSet.contains(newItem.id)) {
                store.state.personalAreaState.experience!.add(newItem);
              }
            }
            next(action);
          });
        }
    
  • 方案3:直接用新列表覆盖旧列表
    你也可以不用addAll方法,直接把新拉取到的列表赋值给全局存储的对应字段,直接完成全量替换,逻辑更简洁:
    // 替换原来的addAll逻辑为直接赋值
    store.state.personalAreaState.experience = experience;
    
    注意如果你的experience字段定义为不可空,需要提前做好空值兼容处理。

内容的提问来源于stack exchange,提问作者Alberto R.C

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.09.30 00:54:03