【问题标题】:Each TIme i call my listView the content is duplicated每次我打电话给我的 listView 内容都是重复的
【发布时间】:2021-12-07 03:32:06
【问题描述】:

我有一个视图,我在其中显示列表中的所有项目。当我再次进入该视图时,所有内容都是重复的。这是我的中间件的源代码,如果我从数据库中获取内容:

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!.addAll(experience);
        next(action);
      });
    }

我想知道我怎样才能让它停止。

【问题讨论】:

  • 但我想调用它更多次,因为我可以从列表中删除项目并添加和编辑。
  • 我只想添加一些不添加现有方法的方法。

标签: flutter dart listview duplicates


【解决方案1】:

如果您需要多次调用该方法,您必须在添加数据库数据之前清除store.state.personalAreaState.experience。像这样:

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(); //<-- clear list before add data
        store.state.personalAreaState.experience!.addAll(experience);
        next(action);
      });
    }

【讨论】:

  • 在第一个实例中执行该列表有时会显示为空
  • 何时出现空响应数据库有数据?这是我认为列表为空的唯一情况
猜你喜欢
  • 1970-01-01
  • 2013-08-05
  • 1970-01-01
  • 2021-04-27
  • 1970-01-01
  • 2012-10-03
  • 2016-01-18
  • 1970-01-01
  • 2022-11-23
相关资源
最近更新 更多