【问题标题】:How to fix _castError List<dynamic>' is not a subtype of type 'String' in type cast如何修复 _castError List<dynamic>' 不是类型转换中“String”类型的子类型
【发布时间】:2021-08-29 13:01:24
【问题描述】:

我收到此错误列表'不是类型转换中'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

【问题讨论】:

    标签: flutter dart


    【解决方案1】:

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

     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'];
          }
        });
      }
    

    【讨论】:

    • http 中的 post 方法期望正文为字符串。 jsonEncode 会将您的 Map 转换为字符串,以便它可以插入到您的请求正文中。
    猜你喜欢
    • 1970-01-01
    • 2020-03-03
    • 1970-01-01
    • 2022-08-16
    • 1970-01-01
    • 1970-01-01
    • 2021-01-03
    • 2021-06-23
    • 2021-10-23
    相关资源
    最近更新 更多