首页 > 解决方案 > 当应用程序移至后台状态并再次移至前台状态时,无限列表会导致重复

问题描述

我正在尝试使用ListView构建器从 api 加载一堆名称。我api有一个名为 index 的参数,每次用户到达列表末尾时都需要增加 50,所以我将 a 附加ScrollController到我的ListView. 开始时 index 的值为 0。

我打电话给api第一个initState

当用户到达列表末尾时,Folwoing 是我的代码

 scrollController.addListener(() {
      if (scrollController.position.pixels ==
          scrollController.position.maxScrollExtent) {
        index += 50;

        //Calling the api again here
      }
    });

现在使用这种方式列表加载正常。假设用户加载了所有数据并假设索引为 250,现在用户决定将应用程序置于后台,一段时间后再次打开应用程序,最后 50 个项目再次添加到我的列表中,我没有明白为什么。

我正在使用StreamBuilderwithbloc模式

if (snapshot.data != null) {
 studentList.addAll(snapshot.data.studentList);
}

我厌倦了独特的运营商,但它不适用于我的情况

Observable<StudentListModel> get studentList => _studentList.stream.distinct();

标签: listviewdartflutterpaging

解决方案


我猜这是你的问题。

if (snapshot.data != null) {
  studentList.addAll(snapshot.data.studentList);
}

您将在前 50 个检索到的学生之上添加 100 个学生,其中 50 个是重复的。您可以将代码更改为:

if (snapshot.data != null) {
  studentList = snapshot.data.studentList;
}

推荐阅读