network_module.dart 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. import 'package:dio/dio.dart';
  2. class _NetworkModule {
  3. static Dio _createDefaultDio() {
  4. Dio dio = Dio(BaseOptions(
  5. /// 发送数据的超时设置。
  6. ///
  7. /// 超时时会抛出类型为 [DioExceptionType.sendTimeout] 的
  8. /// [DioException]。
  9. ///
  10. /// `null` 或 `Duration.zero` 即不设置超时。
  11. //Duration ? sendTimeout;
  12. /// 接收数据的超时设置。
  13. ///
  14. /// 这里的超时对应的时间是:
  15. /// - 在建立连接和第一次收到响应数据事件之前的超时。
  16. /// - 每个数据事件传输的间隔时间,而不是接收的总持续时间。
  17. ///
  18. /// 超时时会抛出类型为 [DioExceptionType.receiveTimeout] 的
  19. /// [DioException]。
  20. ///
  21. /// `null` 或 `Duration.zero` 即不设置超时。
  22. //Duration ? receiveTimeout;
  23. /// 可以在 [Interceptor]、[Transformer] 和
  24. /// [Response.requestOptions] 中获取到的自定义对象。
  25. //Map<String, dynamic> ? extra;
  26. /// HTTP 请求头。
  27. ///
  28. /// 请求头的键是否相等的判断大小写不敏感的。
  29. /// 例如:`content-type` 和 `Content-Type` 会视为同样的请求头键。
  30. //Map<String, dynamic> ? headers;
  31. /// 是否保留请求头的大小写。
  32. ///
  33. /// 默认值为 false。
  34. ///
  35. /// 该选项在以下场景无效:
  36. /// - XHR 不支持直接处理。
  37. /// - 按照 HTTP/2 的标准,只支持小写请求头键。
  38. //bool ? preserveHeaderCase;
  39. /// 表示 [Dio] 处理请求响应数据的类型。
  40. ///
  41. /// 默认值为 [ResponseType.json]。
  42. /// [Dio] 会在请求响应的 content-type
  43. /// 为 [Headers.jsonContentType] 时自动将响应字符串处理为 JSON 对象。
  44. ///
  45. /// 在以下情况时,分别使用:
  46. /// - `plain` 将数据处理为 `String`;
  47. /// - `bytes` 将数据处理为完整的 bytes。
  48. /// - `stream` 将数据处理为流式返回的二进制数据;
  49. //ResponseType? responseType;
  50. /// 请求的 content-type。
  51. ///
  52. /// 请求默认的 `content-type` 会由 [ImplyContentTypeInterceptor]
  53. /// 根据发送数据的类型推断。它可以通过
  54. /// [Interceptors.removeImplyContentTypeInterceptor] 移除。
  55. //String? contentType;
  56. /// 判断当前返回的状态码是否可以视为请求成功。
  57. //ValidateStatus? validateStatus;
  58. /// 是否在请求失败时仍然获取返回数据内容。
  59. ///
  60. /// 默认为 true。
  61. //bool? receiveDataWhenStatusError;
  62. /// 参考 [HttpClientRequest.followRedirects]。
  63. ///
  64. /// 默认为 true。
  65. //bool? followRedirects;
  66. /// 当 [followRedirects] 为 true 时,指定的最大重定向次数。
  67. /// 如果请求超出了重定向次数上线,会抛出 [RedirectException]。
  68. ///
  69. /// 默认为 5。
  70. //int? maxRedirects;
  71. /// 参考 [HttpClientRequest.persistentConnection]。
  72. ///
  73. /// 默认为 true。
  74. //bool? persistentConnection;
  75. /// 对请求内容进行自定义编码转换。
  76. ///
  77. /// 默认为 [Utf8Encoder]。
  78. //RequestEncoder? requestEncoder;
  79. /// 对请求响应内容进行自定义解码转换。
  80. ///
  81. /// 默认为 [Utf8Decoder]。
  82. //ResponseDecoder? responseDecoder;
  83. /// 当请求参数以 `x-www-url-encoded` 方式发送时,如何处理集合参数。
  84. ///
  85. /// 默认为 [ListFormat.multi]。
  86. //ListFormat? listFormat;
  87. ));
  88. dio.interceptors.add(LogInterceptor(requestBody: true, responseBody: true));
  89. return dio;
  90. }
  91. }
  92. final defaultDio = _NetworkModule._createDefaultDio();