首页 > 解决方案 > 如何修复 _castError 列表' 不是类型转换中类型 'String' 的子类型

问题描述

我收到此错误列表'不是类型转换中'String'类型的子类型,目前我正在正文中发送表单数据,因为服务器不接受任何原始请求

Future orderConfirmation(CartConfirmationModel cd, orderNotifyData) {
    var dataMap = new Map<dynamic, dynamic>();

    var orders = [];

    for (var item in orderNotifyData.items) {
      var productMap = {
        'product_id': item.productId,
        'qty': item.qty,
      };
      orders.add(productMap);
    }

    dataMap['customer_id'] = cd.customerId;

    **//dataMap['orderLine'] = orders; // if i add orders to dataMap i get this error**

    return http
        .post(API + '/orders/create', headers: key, body: dataMap)
        .then((data) {
      if (data.statusCode == 200) {
        final jsonData = json.decode(data.body);

        return jsonData['Result'];
      }
    });
  }

// 服务器响应,表示服务器成功接收并返回状态 200

Map (2 items)
1:"Result" -> Map (1 item)
 key:"Code"
 value:200
0:"Code" -> 200

标签: flutterdart

解决方案


您没有对请求的正文进行编码。

 Future orderConfirmation(CartConfirmationModel cd, orderNotifyData) {
    var dataMap = new Map<dynamic, dynamic>();
    var orders = [];
    for (var item in orderNotifyData.items) {
      var productMap = {
        'product_id': item.productId,
        'qty': item.qty,
      };
      orders.add(productMap);
    }

    dataMap['customer_id'] = cd.customerId;

    dataMap['orderLine'] = orders;

    return http
        .post(API + '/orders/create', headers: key, body: jsonEncode(dataMap))
        .then((data) {
      if (data.statusCode == 200) {
        final jsonData = json.decode(data.body);

        return jsonData['Result'];
      }
    });
  }

推荐阅读