当前位置: 首页 > 知识库问答 >
问题:

Flutter Qs:未处理的异常:类型_InternalLinkedHashMap不是类型Iterable的子类型

况庆
2023-03-14

这里是我转换json响应的地方,我只想获取一些响应并在我的应用程序上显示,

Future <List<PNLDATA>>_getPNLCONTENT() async {
    var biller = "PNLpre";
    var idPel = ContentPNLController.text;
    var params = ContentPNLNumController.text.replaceAll(".", "");
    var data = await ContentPre(Bill, ID, prm);
    var jsonData = json.decode(data);
    var jsonProduk = jsonData["VALUE"];
    List< PNLDATA > pnldata = [];
    for (var data in jsonProduk) {
      pnldata.add(
          new PNLDATA(
          data['dt_name'], data['dt_trs'], data['dt_GTag'], data['dt_GAd'], data["dt_Gsum"])
      );
    }
    print(pnldata);
    return pnldata;
  }

错误消息

[VERBOSE-2:ui_dart_state.cc(199)] Unhandled Exception: type '_InternalLinkedHashMap<String, dynamic>' is not a subtype of type 'Iterable<dynamic>'

Json回应:

flutter: {
    "RC": "00",
    "CMD": "PNLContent",
    "VALUE": {
        "dt_status": "SUCCESS",
        "dt_name": "Caramel",
        "dt_trs": "0000000001",
        "dt_GTag": "00002",
        "dt_GAd": "Macchiato",
        "dt_Gsum": "22500",
        "dt_Urs" : "22212",
        },
}

PNLDATA类

class PNLDATA {
  final String dt_name;
  final String dt_trs;
  final String dt_GTag;
  final String dt_GAd;
  final String dt_Gsum;

  PNLDATA(this. dt_name, this. dt_trs, this.dt_GTag,this.dt_dt_GAd,this.dt_Gsum);
}

共有2个答案

姜博
2023-03-14

发生错误是因为您的jsonProduk类型为Map。这意味着你不能使用for循环。由于您的回复来自类型Map,您必须将方法更改为以下内容。

Future<PNLDATA> _getPNLCONTENT() async {
  var biller = "PNLpre";
  var idPel = ContentPNLController.text;
  var params = ContentPNLNumController.text.replaceAll(".", "");
  var data = await ContentPre(Bill, ID, prm);
  var jsonData = json.decode(data);
  var jsonProduk = jsonData["VALUE"];

  final PNLDATA pnldata = PNLDATA(
    data['dt_name'],
    data['dt_trs'],
    data['dt_GTag'],
    data['dt_GAd'],
    data["dt_Gsum"],
  );

  print(pnldata);
  return pnldata;
}
闻人冷勋
2023-03-14

我看到一些错误。首先,您将在jsonProduk处获得一个Map值并对其进行迭代。您需要遍历列表

如果您想返回一个值,因为json将始终包含1个值,您可以尝试以下代码:

Future<PNLDATA> _getPNLCONTENT() async {
    var biller = "PNLpre";
    var idPel = ContentPNLController.text;
    var params = ContentPNLNumController.text.replaceAll(".", "");
    var data = await ContentPre(Bill, ID, prm);
    var jsonData = json.decode(data);
    var jsonProduk = jsonData["VALUE"];
    final PNLDATA pnldata =
          new PNLDATA(
          jsonData['dt_name'] as String,
          jsonData['dt_trs'] as String,
          jsonData['dt_GTag'] as String,
          jsonData['dt_GAd'] as String,
          jsonData["dt_Gsum"] as String); 
// I think this map requires explicit casting. You can check to see if it doesn't work without the casting
      
    print(pnldata);
    return pnldata;
  }

如果您想返回一个列表,您可能应该使用列表结构保存一个json。大概是这样的:

{
      "RC": "00",
      "CMD": "PNLContent",
      "VALUES": [
        {
          "dt_status": "SUCCESS",
          "dt_name": "Caramel",
          "dt_trs": "0000000001",
          "dt_GTag": "00002",
          "dt_GAd": "Macchiato",
          "dt_Gsum": "22500",
          "dt_Urs": "22212",
        }
      ],
    };

这样,您可以尝试实现类似于此的代码。

Future<List<PNLDATA>>_getPNLCONTENT() async {
    var biller = "PNLpre";
    var idPel = ContentPNLController.text;
    var params = ContentPNLNumController.text.replaceAll(".", "");
    var data = await ContentPre(Bill, ID, prm);
    var jsonData = json.decode(data);
    var jsonProduk = jsonData["VALUES"];
    List<PNLDATA> pnldata = [];
    for (var data in jsonProduk) {
      pnldata.add(
          new PNLDATA(
          data['dt_name'] as String, data['dt_trs'] as String, data['dt_GTag'] as String, data['dt_GAd'] as String, data["dt_Gsum"] as String)
      );
    }
    print(pnldata);
    return pnldata;
  }
 类似资料: