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

Flutter POST请求未返回全部结果:260条数据仅保留113条

Flutter POST请求无法获取全部数据:接口返回260条仅保存113条

接口返回约260条列表数据,但通过POST请求后仅保存了113条到本地列表中,怀疑是字符串限制导致,但不确定。

相关代码

请求方法

Future<List<NewChatModel>> getAllChats({@required String userId}) async {
  final response = await http.post(
    Uri.parse(URL),
    headers: <String, String>{
      'Content-Type': 'application/json; charset=UTF-8',
    },
    body: jsonEncode<String, int>{
      'first_user_id': int.parse(userId),
    },
  );
  if (response.statusCode == 200) {
    List<NewChatModel> returnList = [];
    for (var i in jsonDecode(response.body)) {
      returnList.add(NewChatModel.fromJson(i));
    }
    print(returnList.length);
    return returnList;
  } else {
    return null;
  }
}

NewChatModel类

class NewChatModel {
  String id;
  String chatId;
  String messageId;
  String message;
  String messageDate;
  String schoolId;
  String fullName;
  String phone;
  String email;
  String urlProfileImage;
  String birthday;
  String roleId;
  String lastActivity;
  String isOnline;

  NewChatModel(
      {this.id,
      this.chatId,
      this.messageId,
      this.message,
      this.messageDate,
      this.schoolId,
      this.fullName,
      this.phone,
      this.email,
      this.urlProfileImage,
      this.birthday,
      this.roleId,
      this.lastActivity,
      this.isOnline});

  factory NewChatModel.fromJson(dynamic json) {
    return NewChatModel(
      id: json['id'].toString(),
      chatId: json['chat_id'].toString(),
      messageId: json['message_id'].toString(),
      message: json['message'].toString(),
      messageDate: json['message_date'].toString(),
      schoolId: json['school_id'].toString(),
      fullName: json['full_name'].toString(),
      phone: json['phone'].toString(),
      email: json['email'].toString(),
      urlProfileImage: json['urlProfileImage'].toString(),
      birthday: json['birthday'].toString(),
      roleId: json['role_id'].toString(),
      lastActivity: json['last_activity'].toString(),
      isOnline: json['is_online'].toString(),
    );
  }
}

排查与解决建议

  1. 确认接口返回的原始数据长度
    在解析前先打印原始响应的数组长度,判断问题出在前端还是后端:
if (response.statusCode == 200) {
  List<dynamic> rawData = jsonDecode(response.body);
  print('接口返回数据总条数: ${rawData.length}'); // 看是否为260
  // 后续转换逻辑
}

如果打印的长度是113,说明后端返回的数据本身就只有113条,需要和后端确认是否存在分页、数据过滤等限制。

  1. 捕获Model转换时的异常
    你的fromJson方法中所有字段都调用了.toString(),若某个字段为null或类型不匹配,可能导致转换失败但未抛出明显错误,从而跳过该条数据。在循环中添加异常捕获,定位问题数据:
for (var item in rawData) {
  try {
    returnList.add(NewChatModel.fromJson(item));
  } catch (e) {
    print('转换失败的数据项: $item');
    print('错误信息: $e');
  }
}
  1. 确保响应完整读取
    部分大响应可能因默认读取方式被截断,改用stream读取完整响应:
final response = await http.post(...);
if (response.statusCode == 200) {
  // 用stream读取完整响应体
  String fullBody = await response.stream.bytesToString();
  List<dynamic> rawData = jsonDecode(fullBody);
  // 后续逻辑
}
  1. 检查后端分页机制
    很多接口默认启用分页,即使未传分页参数,也会返回固定页数的数据。联系后端确认是否需要传递page、limit等参数来获取全部260条数据。

内容的提问来源于stack exchange,提问作者Artem Kryakushin

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.20 13:16:58