首页 > 解决方案 > 每次我打电话给我的 listView 内容都是重复的

问题描述

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

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);
      });
    }

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

标签: flutterdartlistviewduplicates

解决方案


如果您需要多次调用该方法,则必须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);
      });
    }

推荐阅读