ClientException,我无法打印返回值(请求正文)

ClientException, and i can't print the returned value (the request body)

好吧,我在这里失去了理智, 在我的 flutter 应用程序中,我使用这个函数来执行 post 请求:

 Future<Map> postRequest(String serviceName, Map<String, dynamic> data) async {
    var responseBody = json.decode('{"data": "", "status": "NOK"}');
    try {
      http.Response response = await http.post(
        _urlBase + '$_serverApi$serviceName',
        body: jsonEncode(data),
      );
      if (response.statusCode == 200) {
        responseBody = jsonDecode(response.body);
        //
        // If we receive a new token, let's save it
        //
        if (responseBody["status"] == "TOKEN") {
          await _setMobileToken(responseBody["data"]);

          // TODO: rerun the Post request
        }
      }
    } catch (e) {
      // An error was received
      throw new Exception("POST ERROR");
    }
    return responseBody;
  }

问题是:

  1. 我得到一个 ClientException(不是每次)

  2. 在另一个class中,我将这个函数的结果存储在一个变量中,它应该是return一个Future<Map<dynamic, dynamic>>,当我打印它时它显示:

    I/flutter ( 9001): Instance of 'Future<Map<dynamic, dynamic>>'

但是当我 运行 直接 post 请求时(不使用函数)它起作用了,它显示了我正在等待的消息。

注意:在这两种情况下(功能与否),在服务器端都是一样的。

这是我使用 post 请求的函数:

void _confirm() {
    if (_formKey.currentState.saveAndValidate()) {
      print(_formKey.currentState.value);
      var v = auth.postRequest("se_connecter", _formKey.currentState.value);
      print(v);
    } else {
      print(_formKey.currentState.value);
      print("validation failed");
    }
  }

那么对于第二个问题,我只是做了这些更改:

void _confirm() async {

var v = await auth.postRequest('se_connecter', _formKey.currentState.value);

是的,这很愚蠢。

例外情况是 ssl 加密导致的,所以我将其从后端删除。