首页 > 解决方案 > Flutter 发布请求解析

问题描述

我有一个 POST 请求:

  static String url = 'https://checkout.test.paycom.com/api';
  static Map<String, String> headers = {
    'Host': 'checkout.test.paycom.com',
    'X-Auth': '1234',
    'Cache-Control': 'no-cache'
  };

Future<Map<String, dynamic>> createCard() async {
try {
  Map<String, dynamic> body = {
    "id": '123',
    "method": "receipts.create",
    "params": {
      "amount": '2500',
      "account": {"order_id": '106'}
    }
  }

  final response = await http.post(url, body: body, headers: headers);
  print(response.body);
} catch (e) {
  print(e.toString());
}
return null;
}

并给出错误类型 '_InternalLinkedHashMap<String, Object>' is not a subtype of type 'String' in type cast 我做错了什么?

标签: flutterdart

解决方案


你的身体需要作为字符串,最好的情况是你可以将你的身体转换为 JSON 字符串,如下所示:

  final response = await http.post(url, body: jsonEncode(body), headers: headers);

推荐阅读