获取未处理的异常:FormatException:抖动API响应中输入的意外结束(字符1)

时间:2020-03-27 12:54:18

标签: android flutter dart httprequest

我是Flutter的新手。我正在进行POST API调用,并且期望在其中收到类似{“ status”:“ Saved”}之类的响应,因为我在Postman中得到了正确的信息,但是我遇到了未处理的异常:FormatException:输入的意外结束(字符1)在我的颤动代码中。我在堆栈溢出时检查了类似的问题和答案,但没有一个对我有帮助。

static Future<SaveLocationData> saveLocation(BuildContext context, String url,
      {Map body}) async {
    print("save location pressed $body");

    Map<String, String> requestHeaders = {
      'Content-type': 'application/json',
      'Accept': 'application/json',
    };
    try {
      return post(url, body: jsonEncode(body), headers: requestHeaders)
          .then((response) {


        print("DATA ${response.body}");

        var parsedJson = json.decode(response.body.toString());
        NormalAlert.showAlert(
            context,
            "${parsedJson['status'].toString().replaceAll("\n", " ")}",
            "Save Location");

        final int statusCode = response.statusCode;
        if (statusCode < 200 || statusCode > 400 || json == null) {
          throw new Exception("Error while fetching data");
        }
        return SaveLocationData.fromJson(json.decode(response.body));
      });
    } catch (error) {
      print("erroris $error");
    }
  }

0 个答案:

没有答案