flutter 发送获取请求类型“_InternalLinkedHashMap〈String,Object>”不是类型“String”的子类型时查询参数中出错

km0tfn4u  于 2023-03-09  发布在  Flutter
关注(0)|答案(1)|浏览(123)

我想在get请求中使用queryParameters发送数据,但收到此错误类型“_InternalLinkedHashMap〈String,Object〉"不是类型”String“的子类型

var UriData = Uri.parse(base_url_data + api_url);
  final dataa = {
    "Protocol":"jsonPTS",
    "Packets":[{
      "Id":"1",
      "Type":"ReportGetPumpTransactions",
      "Data":{
        "Pump":"1",
        "DateTimeStart":"2019-05-19T12:45:14",
        "DateTimeEnd":"2019-05-19T13:45:14"
      }
    }]
  };
  final newURI = UriData.replace(queryParameters: dataa);

  print(newURI);
  return http.get(
    newURI,
    headers: <String, String>{
      'Content-Type': 'application/json; charset=UTF-8',
    },
  );
wkftcu5l

wkftcu5l1#

在上述编码器中替换

final newURI = UriData.replace(queryParameters: dataa);

用这个

final newURI = UriData.replace(queryParameters: jsonEncode(dataa));
    • 编辑**

要将数据体作为原始数据发送,您可以尝试以下操作:

var headers = {
    'Content-Type': 'application/json'
    };
    var request = http.Request('GET', Uri.parse(''));
    request.body = json.encode({
    "Protocol": "jsonPTS",
    "Packets": [
        {
        "Id": "1",
        "Type": "ReportGetPumpTransactions",
        "Data": {
            "Pump": "1",
            "DateTimeStart": "2019-05-19T12:45:14",
            "DateTimeEnd": "2019-05-19T13:45:14"
        }
        }
    ]
    });
    request.headers.addAll(headers);

    http.StreamedResponse response = await request.send();

    if (response.statusCode == 200) {
    print(await response.stream.bytesToString());
    }
    else {
    print(response.reasonPhrase);
    }

相关问题