AdapterBaseOptions class

adapter: ZhengZaiHong email:1096877329@qq.com date: 2026-08-05 describe: 适配器无关的全局请求默认配置

AdapterBaseOptions 是 RxNet 的平台无关全局配置类,用于替代 Dio 的 BaseOptions, 确保切换适配器(DioAdapter / HttpAdapter / 自定义适配器)时,全局默认参数依然生效。

每个适配器在接收到请求前,会将 AdapterBaseOptions 的字段映射为自身库的等效参数。 例如:

  • DioAdapter → Dio.BaseOptions
  • HttpAdapter → http.Client 配置 + 请求级覆盖

使用示例

final config = RxNetConfig(
  baseUrl: "https://api.example.com",
  baseOptions: AdapterBaseOptions(
    connectTimeout: Duration(seconds: 10),
    receiveTimeout: Duration(seconds: 30),
    sendTimeout: Duration(seconds: 30),
    headers: {'X-App-Version': '2.0.0'},
    contentType: 'application/json',
    followRedirects: true,
    maxRedirects: 5,
  ),
);

await RxNet.init(config: config);

与 Dio BaseOptions 的映射关系

AdapterBaseOptions Dio BaseOptions HttpAdapter 等效行为
connectTimeout connectTimeout http.Client 无直接支持,通过超时拦截器实现
receiveTimeout receiveTimeout 同上
sendTimeout sendTimeout 同上
headers headers 合并到每个请求的 headers
contentType contentType 设置默认 Content-Type
responseType responseType 解析响应体时的默认行为
followRedirects followRedirects http.Client 无直接支持,忽略
maxRedirects maxRedirects 同上
validateStatus validateStatus 所有适配器统一在 AdapterResponse 处理
receiveDataWhenStatusError 对应字段 通过 extra 传递

Constructors

AdapterBaseOptions({Duration? connectTimeout, Duration? receiveTimeout, Duration? sendTimeout, Map<String, dynamic> headers = const {}, String? contentType, ResponseType responseType = ResponseType.json, bool followRedirects = true, int maxRedirects = 5, bool validateStatus(int statusCode)?, bool receiveDataWhenStatusError = true, Map<String, dynamic> extra = const {}, bool persistentConnection = true})
const
AdapterBaseOptions.empty()
创建一个空的默认配置
factory
AdapterBaseOptions.fromDioBaseOptions(BaseOptions dioBaseOptions)
从 Dio 的 BaseOptions 创建适配器无关配置(向后兼容)
factory

Properties

connectTimeout Duration?
连接超时时间
final
contentType String?
默认 Content-Type
final
extra Map<String, dynamic>
额外的适配器特定配置
final
followRedirects bool
是否跟随重定向
final
hashCode int
The hash code for this object.
no setterinherited
headers Map<String, dynamic>
默认请求头
final
maxRedirects int
最大重定向次数
final
persistentConnection bool
final
receiveDataWhenStatusError bool
非 2xx 状态码时是否仍然接收响应数据
final
receiveTimeout Duration?
接收超时时间
final
responseType ResponseType
默认响应类型
final
runtimeType Type
A representation of the runtime type of the object.
no setterinherited
sendTimeout Duration?
发送超时时间
final
validateStatus bool Function(int statusCode)?
状态码验证回调
final

Methods

applyToRequest(AdapterRequest request) AdapterRequest
将此配置应用到 AdapterRequest 上(填充默认值)
copyWith({Duration? connectTimeout, bool clearConnectTimeout = false, Duration? receiveTimeout, bool clearReceiveTimeout = false, Duration? sendTimeout, bool clearSendTimeout = false, Map<String, dynamic>? headers, String? contentType, bool clearContentType = false, ResponseType? responseType, bool? followRedirects, int? maxRedirects, bool validateStatus(int statusCode)?, bool clearValidateStatus = false, bool? receiveDataWhenStatusError, Map<String, dynamic>? extra}) AdapterBaseOptions
复制并修改
merge(AdapterBaseOptions? other) AdapterBaseOptions
合并两个配置,other 的非 null 字段覆盖当前配置
noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
toString() String
A string representation of this object.
override

Operators

operator ==(Object other) bool
The equality operator.
inherited