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

Flutter Dart中从Postman API嵌套JSON提取数据及修正模型错误

问题分析与解决方案

错误原因解析

1. The argument type 'Iterable' can't be assigned to the parameter type 'List?'

API返回的app是单个嵌套对象(用{}包裹),但你在SplashPost中将app定义为List<dynamic>?,并且在fromJson里尝试用List.from(json["app"])把对象转成数组,这完全不符合数据结构。另外map((x) => x)返回的是Iterable类型,没有转换为List,但核心问题是app的类型定义错误。

2. Undefined name 'version'

API里的version字段嵌套在app对象内部,你直接通过json["version"]访问会找不到该字段(根JSON节点没有version)。同时你的toJson方法里用到的installTimeStamp等变量,在SplashPost类中根本未定义——这些都是app对象的属性。


修复步骤

步骤1:创建独立的App数据模型类

因为app是嵌套对象,单独抽成类更符合面向对象规范,也方便维护:

class App {
  String? version;
  String? installTimeStamp;
  String? uninstallTimeStamp;
  String? downloadTimeStamp;

  App({
    this.version,
    this.installTimeStamp,
    this.uninstallTimeStamp,
    this.downloadTimeStamp,
  });

  factory App.fromJson(Map<String, dynamic> json) => App(
        version: json["version"],
        installTimeStamp: json["installTimeStamp"],
        uninstallTimeStamp: json["uninstallTimeStamp"],
        downloadTimeStamp: json["downloadTimeStamp"],
      );

  Map<String, dynamic> toJson() => {
        "version": version,
        "installTimeStamp": installTimeStamp,
        "uninstallTimeStamp": uninstallTimeStamp,
        "downloadTimeStamp": downloadTimeStamp,
      };
}

步骤2:修正SplashPost类

  • 将List<dynamic>? app改为App? app,匹配API返回的对象类型
  • 移除SplashPost中多余的String? version字段(该字段属于App类)
  • 修正fromJson里的app解析逻辑,用App.fromJson(json["app"])解析嵌套对象
  • 修正lat和long的类型:API返回的是数字,将String?改为double?
  • 完善toJson方法,包含所有根节点字段,并嵌套app的JSON转换

修正后的代码:

class SplashPost {
  String? deviceType;
  String? deviceId;
  String? deviceName;
  String? deviceOSVerion;
  String? deviceIPAddress;
  double? lat;
  double? long;
  String? buyer_gcmid;
  String? buyer_pemid;
  App? app;

  SplashPost({
    this.deviceType,
    this.deviceIPAddress,
    this.deviceId,
    this.deviceName,
    this.deviceOSVerion,
    this.app,
    this.buyer_gcmid,
    this.buyer_pemid,
    this.lat,
    this.long,
  });

  factory SplashPost.fromJson(Map<String, dynamic> json) => SplashPost(
        deviceIPAddress: json["deviceIPAddress"],
        deviceType: json["deviceType"],
        deviceId: json["deviceId"],
        deviceName: json["deviceName"],
        deviceOSVerion: json["deviceOSVersion"],
        lat: json["lat"]?.toDouble(),
        long: json["long"]?.toDouble(),
        buyer_gcmid: json["buyer_gcmid"],
        buyer_pemid: json["buyer_pemid"],
        app: json["app"] != null ? App.fromJson(json["app"]) : null,
      );

  Map<String, dynamic> toJson() => {
        "deviceType": deviceType,
        "deviceId": deviceId,
        "deviceName": deviceName,
        "deviceOSVersion": deviceOSVerion,
        "deviceIPAddress": deviceIPAddress,
        "lat": lat,
        "long": long,
        "buyer_gcmid": buyer_gcmid,
        "buyer_pemid": buyer_pemid,
        "app": app?.toJson(),
      };
}

额外说明

  • 如果某些字段是接口必传项,可以在构造函数中保留required修饰符;如果字段可能为空,保持可选参数即可。
  • 访问version字段时,通过SplashPost对象.app?.version即可获取。

内容的提问来源于stack exchange,提问作者Vishnu Kumar

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.28 17:41:06