doPost static method

Future<FastHttpResponse> doPost({
  1. required String url,
  2. dynamic body,
  3. Map<String, dynamic> headers = const {},
  4. bool isReconnectStrategyStart = false,
  5. int reconnectTime = 0,
  6. String? hud,
})

Implementation

static Future<FastHttpResponse> doPost({
  required String url,
  body,
  Map<String, dynamic> headers = const {},
  bool isReconnectStrategyStart = false,
  int reconnectTime = 0,
  String? hud,
}) async {
  adio.BaseOptions options = adio.BaseOptions(
    connectTimeout: CONNECT_TIMEOUT,
    receiveTimeout: RECEIVE_TIMEOUT,
    headers: headers,
    contentType: headers.isNotEmpty ? headers['Content-Type'] : "",
    baseUrl: fastDio?.options.baseUrl ?? '',
  );

  FormData? formData;

  if (headers.isNotEmpty &&
      headers['Content-Type'] == "application/formData") {
    formData = FormData.fromMap(body);
  }

  Map result = {};

  options.headers = fastDio?.options.headers;

  Dio dio = fastDio ?? Dio(options);
  FastHttpResponse httpResponse;

  //如果要提示加载中之类的hud
  if (hud != null && hud.isNotEmpty) {
    EasyLoading.show(status: hud);
  }

  adio.Response response = await dio
      .post(
    url,
    data: formData ?? body,
  )
      .catchError((e) async {
    DioError error = e;

    print('catchError => ${error.message}');

    if (reconnectTime > 0 &&
        isReconnectStrategyStart &&
        (error.type == DioErrorType.sendTimeout ||
            error.type == DioErrorType.receiveTimeout ||
            error.type == DioErrorType.connectTimeout)) {
      print('start reconnect reconnectTime left: $reconnectTime');
      httpResponse = await doPost(
        url: url,
        body: body,
        headers: headers,
        isReconnectStrategyStart: isReconnectStrategyStart,
        reconnectTime: reconnectTime--,
      );
      // ignore: invalid_return_type_for_catch_error
      return httpResponse;
    } else {
      result = {
        "msg": "Server busy,please try again later",
        "code": -1,
        "msgCode": "1"
      };
      httpResponse = FastHttpResponse(jsonEncode(result), -1, {}, result);
      // ignore: invalid_return_type_for_catch_error
      return httpResponse;
    }
  });

  if (hud != null && hud.isNotEmpty) {
    EasyLoading.dismiss();
  }

  if (response.statusCode == HttpStatus.ok) {
    var data = response.data;
    if (data is String) {
      result = jsonDecode(data);
    } else if (data is Map) {
      result = data;
    }
  } else {
    result = {
      "msg": "Server busy",
      "code": response.statusCode,
      "msgCode": "1"
    };

    if (fastDio != null) {
      throw DioError(
        requestOptions: response.requestOptions,
        response: response,
      ); //新版本 兼容旧版本
    }
  }
  httpResponse = FastHttpResponse(
      jsonEncode(result), response.statusCode, response.headers.map, result);

  return httpResponse;
}