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

Dart中List<Map<String,String>>长度受限问题求助

问题描述

定义了如下Dart代码初始化列表:

List<Map<String, String>> listItem = <Map<String, String>>[];

随后通过for循环向列表添加Map元素:

for (int i = 0; i < items.length; i++) { 
  Map<String, String> val = { 
    "OrderState": "", 
    "LineID": items[i].id.toString(), 
    "ClientID": "idCliente!!", 
    "ProductReference": items[i].ref.toString(), 
    "Color": items[i].cor.toString(), 
    "Size": items[i].tamanho.toString(), 
    "Quantity": items[i].quantity.toString(), 
    "UnitPriceWithoutTax": items[i].priceWithoutTax.toString(), 
    "UnitPriceWithTax": items[i].price.toString(), 
    "ShippingAmount": precoEnvio.toString(), 
    "DiscountAmount": "0", 
    "OrderReference": "", 
    "CarrierID": idTrans.toString(), 
    "Comments": coment.toString(), 
    "ProductDescription": "items[i].descricao.toString()", 
    "ShippingName": "envioNome!!", 
    "ShippingAddress1": "envioAdr1!!", 
    "ShippingAddress2": "envioAdr2!!", 
    "ShippingPostalCode": "envioPostalCode!!", 
    "ShippingCity": "envioCidade!!", 
    "shippingPhone": "envioTele!!", 
    "BillingName": "envioNome!!", 
    "BillingAddress1": "envioAdr1!!", 
    "BillingAddress2": "envioAdr2!!", 
    "BillingPostalCode": "envioPostalCode!!", 
    "BillingCity": "envioCidade!!", 
    "BillingVAT": "envioNif!!", 
    "BillingEmail": "envioEmail!!", 
    "BillingPhone": "envioTele!!", 
    "PaymentID": idPagamento.toString(), 
    "PaymentInfo1": tele.toString(), 
    "PaymentInfo2": "", 
    "PaymentInfo3": "", 
    "PaymentInfo4": "", 
    "PaymentInfo5": "", 
    "AF1": "", 
    "AF2": "", 
    "AF3": "", 
    "AF4": "", 
    "AF5": "", 
    "AF6": "", 
    "AF7": "", 
    "AF8": "", 
    "AF9": "", 
    "AF10": "" 
  }; 
  listItem.add(val); 
}

运行时发现列表无法正常添加更多元素,处理第二个元素的"ShippingPo"字段时流程停止,需要让列表支持2到20甚至更多元素的添加。

解决方案

首先明确:Dart中默认通过<Map<String, String>>[]初始化的List本身没有长度限制,它会自动扩容,问题出在代码的其他环节,而非List的容量限制。以下是具体排查和修复方向:

  • 修正字段拼写与赋值错误
    你提到的"ShippingPo"字段在代码中实际是"ShippingPostalCode",如果后续逻辑中引用了错误的字段名会导致异常中断流程;另外ProductDescription字段当前是赋值字符串字面量,而非调用toString()获取实际值,需修正为:

    "ProductDescription": items[i].descricao?.toString() ?? "",
    

    这里用?.和??处理descricao为null的情况,避免抛出空指针异常。

  • 添加异常捕获定位问题
    在循环内部加入try-catch块,捕获并打印异常信息,就能精准定位是哪一步导致流程停止:

    for (int i = 0; i < items.length; i++) { 
      try {
        Map<String, String> val = { 
          // ... 你的Map内容(已修正ProductDescription)
        }; 
        listItem.add(val); 
      } catch (e, stackTrace) {
        print("处理第${i+1}个元素出错: $e");
        print("堆栈信息: $stackTrace");
      }
    }
    
  • 验证items的有效性
    检查items.length是否确实大于1,如果items本身只有1个元素,循环自然只会执行一次;同时逐个确认items中元素的属性(如id、ref、cor等)是否为null,避免调用toString()时抛出NoSuchMethodError。

  • 简化循环写法(可选)
    确认items所有元素都有效后,可使用更简洁的map+toList()写法,减少手动循环的潜在错误:

    listItem = items.map((item) {
      return {
        "OrderState": "", 
        "LineID": item.id?.toString() ?? "",
        "ClientID": "idCliente!!", 
        "ProductReference": item.ref?.toString() ?? "",
        // ... 其他字段(依次修正可能的空值问题)
        "ProductDescription": item.descricao?.toString() ?? "",
      };
    }).toList();
    

内容的提问来源于stack exchange,提问作者Rafael Reis

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.17 00:40:36