request<T> method

Future<DioResult<T>> request<T>(
  1. String path, {
  2. DioMethod method = DioMethod.get,
  3. Map<String, dynamic>? parmas,
  4. dynamic data,
  5. CancelToken? cancelToken,
  6. Options? options,
  7. Dio? dio,
  8. Map<String, dynamic>? extra = const {'needCache' : false},
  9. ProgressCallback? onSendProgress,
  10. ProgressCallback? onReceiveProgress,
})

Implementation

Future<DioResult<T>> request<T>(String path,
    {DioMethod method = DioMethod.get,
    Map<String, dynamic>? parmas,
    data,
    CancelToken? cancelToken,
    Options? options,
    Dio? dio,
    Map<String, dynamic>? extra = const {'needCache': false},
    ProgressCallback? onSendProgress,
    ProgressCallback? onReceiveProgress}) async {
  const methodValues = {
    DioMethod.get: 'get',
    DioMethod.post: 'post',
    DioMethod.put: 'put',
    DioMethod.delete: 'delete',
    DioMethod.patch: 'patch',
    DioMethod.head: 'head'
  };
  assert(_dio != null, "请调用init方法初始化网络请求");
  options ??= Options(
      method: methodValues[method],
      contentType: Headers.jsonContentType,
      extra: extra);

  try {
    Response response = await (dio ?? _dio!).request(path,
        data: data,
        queryParameters: parmas,
        cancelToken: cancelToken ?? _cancelToken,
        options: options,
        onSendProgress: onSendProgress,
        onReceiveProgress: onReceiveProgress);
    return DioResult(response: response.data);
  } on DioException catch (e) {
    // debugInfo('请求失败 $e');
    return DioResult(e: e);
  } catch (e) {
    return DioResult(unknown: e);
  }
}