listview - 当应用程序移至后台状态并再次移至前台状态时,无限列表会导致重复
问题描述
我正在尝试使用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 个项目再次添加到我的列表中,我没有明白为什么。
我正在使用StreamBuilder
withbloc
模式
if (snapshot.data != null) {
studentList.addAll(snapshot.data.studentList);
}
我厌倦了独特的运营商,但它不适用于我的情况
Observable<StudentListModel> get studentList => _studentList.stream.distinct();
解决方案
我猜这是你的问题。
if (snapshot.data != null) {
studentList.addAll(snapshot.data.studentList);
}
您将在前 50 个检索到的学生之上添加 100 个学生,其中 50 个是重复的。您可以将代码更改为:
if (snapshot.data != null) {
studentList = snapshot.data.studentList;
}
推荐阅读
- pandas - 根据熊猫数据框中的条件交换 2 个日期时间列
- laravel - 收到错误:localhost 将您重定向了太多次
- c# - 从 Enum 返回键值对列表
- python - 在数据框中添加新列时如何获取新值?熊猫
- amazon-web-services - AWS S3 (ap-south-1) 为 Hudi DeltaStreamer 作业返回错误请求
- angular - Router getCurrentNavigation():改变它返回的值是否安全?
- excel - Excel VBA - 如何查找和替换 30k 字符串上的单引号?
- python-2.7 - 用于 Python 2.7 的更好看的 Tkinter 文件对话框
- webpack - 如何在 Percy 中配置模块名称别名
- python - 列表字典python中的唯一元素错误