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

Flutter中如何实现ListView加载新数据追加至旧数据列表末尾?

修复分页加载时旧数据被覆盖的问题

问题核心在于每次调用getNotificationList获取下一页数据时,直接用新数据替换了旧的notificationListData集合,未执行追加操作。以下是具体修改方案:

1. 修改Bloc中的数据处理逻辑

在getNotificationList方法中区分**刷新(重新获取第一页)和加载更多(获取后续页)**场景:

  • 当skip=0(刷新)时,直接替换旧数据
  • 当skip>0(加载更多)时,将新数据追加到旧数据集合末尾

修改后的getNotificationList代码:

getNotificationList({int skip = 0, int fetchLimitCount = 20}) async {
    try {
      await _notificationRepository
          .getNotificationList(
              notificationListEndPoint: END_POINT_NOTIFICATION_LIST,
              startingNumber: skip,
              fetchCount: fetchLimitCount)
          .then(
        (responseValue) {
          _notificationListController.sink.add(MessageState.loading);
          if (responseValue?.messageState == MessageState.data) {
            final newData = responseValue!.data?.data ?? [];
            if (skip == 0) {
              // 刷新操作:清空旧数据,替换为新数据
              notificationListModel = responseValue.data;
              notificationListData = newData;
            } else {
              // 加载更多:追加新数据到旧集合
              notificationListData.addAll(newData);
              // 更新全局统计数据,保持与接口返回一致
              notificationListModel = responseValue.data;
            }
            _notificationListController.sink.add(MessageState.data);
          } else if (responseValue?.messageState == MessageState.requestError) {
            errorHandlingModel = responseValue?.data;
            _notificationListController.sink.add(MessageState.requestError);
          } else if (responseValue?.messageState == MessageState.serverError) {
            _notificationListController.sink.add(MessageState.serverError);
          }
        },
      );
    } catch (e) {
      errorMessage = e.toString();
      _notificationListController.sink.add(MessageState.exception);
    }
  }

2. 优化SmartRefresher的onLoading逻辑

原代码先更新skipCount再判断是否有数据,若请求失败会导致skipCount错误累加,建议调整为先计算下一页的skip值,请求成功后再更新skipCount:

onLoading: () {
  final nextSkip = widget.notificationBloc.skipCount + widget.notificationBloc.limitCount;
  if (widget.notificationBloc.notificationListModel!.total <= nextSkip) {
    _refreshController.loadNoData();
  } else {
    widget.notificationBloc
        .getNotificationList(
            skip: nextSkip,
            fetchLimitCount: widget.notificationBloc.limitCount)
        .then((value) {
          // 请求成功后更新skipCount
          widget.notificationBloc.skipCount = nextSkip;
          _refreshController.loadComplete();
        })
        .catchError((_) {
          // 请求失败时不更新skipCount,允许重试
          _refreshController.loadFailed();
        });
  }
},

3. 确认ListView.builder数据绑定正确

确保ListView.builder的itemCount使用notificationListData.length,每个item对应notificationListData[index],新数据追加后列表会自动更新。


内容的提问来源于stack exchange,提问作者La Pyae

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.28 05:13:27