HeaderPlugin class

固定头部插件

自动添加预配置的请求头,支持异步获取。

基本用法

HeaderPlugin(
  providers: [
    () async => {'X-App-Version': '1.0.0'},
    () async {
      final token = await TokenManager.getToken();
      return {'Authorization': token != null ? 'Bearer $token' : null};
    },
  ],
)

单个 Header 用法

HeaderPlugin.single({
  'Authorization': () async {
    final token = await TokenManager.getToken();
    return token != null ? 'Bearer $token' : null;
  },
  'X-Device-Id': () async => await DeviceInfo.getId(),
})

skipEmpty 选项

skipEmpty: true 时,会跳过 key 或 value 为空字符串的 header。 默认 skipEmpty: true

priority = 20

Inheritance

Constructors

HeaderPlugin({required List<AsyncHeaderProvider> providers, bool skipEmpty = true})
从提供器列表创建
HeaderPlugin.single({required Map<String, AsyncHeaderValueProvider> headers, bool skipEmpty = true})
从单个 Header 提供器 Map 创建
factory

Properties

hashCode int
The hash code for this object.
no setterinherited
name String
插件名称(用于调试)
no setteroverride
priority int
优先级,数字越小越先执行,默认 100
no setteroverride
runtimeType Type
A representation of the runtime type of the object.
no setterinherited
skipEmpty bool
是否跳过空值(null 或空字符串)
final

Methods

noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
onError(RequestContext ctx, SmartError error) Future<void>
错误发生时调用
inherited
onFinish(RequestContext ctx) Future<void>
请求结束后调用(无论成功失败都会执行)
inherited
onRequest(RequestContext ctx, RequestOptions options) Future<void>
请求发送前调用(可修改 RequestOptions)
override
onResponse(RequestContext ctx, Response response) Future<void>
响应接收后调用
inherited
onStart(RequestContext ctx) Future<void>
请求开始前调用
inherited
toString() String
A string representation of this object.
inherited

Operators

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