request method

  1. @Doc.new(message: "向服务器发起http请求")
Future request({
  1. bool showErrorMsg = true,
  2. String? loadingText,
  3. String contentType = "",
  4. Map<String, dynamic>? headers,
  5. bool showDefaultLoading = true,
  6. dynamic data,
  7. ResponseType? responseType,
  8. bool? nullParams,
  9. RequestEncoder? requestEncoder,
  10. DioStart? dioStart,
})
inherited

Implementation

@Doc(message: "向服务器发起http请求")
Future<dynamic> request(
    {bool showErrorMsg = true,
    String? loadingText,
    String contentType = "",
    Map<String, dynamic>? headers,
    bool showDefaultLoading = true,
    dynamic data,
    ResponseType? responseType,
    bool? nullParams,
    RequestEncoder? requestEncoder,
    DioStart? dioStart}) async {
  try {
    if (showDefaultLoading) {
      showLoading(loadingText: loadingText);
    }

    final dio = getDio();
    intrtceptors.add(ErrorInterceptor());
    dio.interceptors.addAll(intrtceptors);
    final contentTypeStr = contentType.isNotEmpty ? contentType : (httpMethod == HttpMethod.post ? ContentType.json.value : null);
    final bodyParams = formData.files.isNotEmpty ? formData : (data ?? params);
    dioStart?.call(dio,_host + url);
    final response = await dio.request(
      (_host + url),
      options: Options(
          method: methed,
          contentType: httpMethod == HttpMethod.probuf ? kProtobufContentType : contentTypeStr,
          headers: headers,
          responseType: responseType,
          requestEncoder: requestEncoder),
      queryParameters: nullParams == true ? null : data ?? params,
      data: bodyParams,
    );
    if (showDefaultLoading) {
      closeLoading();
    }

    if (response.statusCode == 200) {
      final data = response.data;
      return data;
    }
  } on DioError catch (e, s) {
    kLogErr("出现异常:${e.error.runtimeType}\n$s");
    if (showDefaultLoading) {
      closeLoading();
    }
    throw e.error as AppException;
  } catch (e,s) {
    kLogErr('error:$e\n$s');
    throw AppException.appError();
  }
  throw AppException.appError();
}