DIO响应解码问题

时间:2019-01-25 13:38:16

标签: android json http dart flutter

我正在使用val formData = MultipartFormData( dataParts = Map("" -> Seq("dummydata")), files = Seq(file), badParts = Seq()) 发出Dio请求

HTTP
  

我正在回复的答复

  var dio = Dio();
    var response =
        await dio.get(URL);
    final responseBody = json.decode(response.data);

    final statusCode = response.statusCode;

    if (statusCode != 200 || responseBody == null) {
      print("status code:$statusCode");
      throw new ServerExceptionHandler(
          "An error ocurred : [Status Code : $statusCode]", statusCode);
    }

但是{ "x_id": "home" } 抛出了异常。

json.decode(response.data)

奇怪的是,当我将type '_InternalLinkedHashMap<String, dynamic>' is not a subtype of type 'String' 替换为Dio时,http.get(url)的工作原理与相同的解决方案完全相同

2 个答案:

答案 0 :(得分:2)

您的响应数据已经是一个地图,因此您可以轻松地做到这一点:

var response = await dio.get(_url);
var responseBody = response.data;
print(responseBody);

这是显式类型:

Response<Map> response = await dio.get(_url);
Map responseBody = response.data;
print(responseBody);

答案 1 :(得分:0)

遵循此方法 将响应作为字符串。现在您可以对其进行解码...

19c