ResponseParserPlugin class

响应解析插件

自动解析后端统一响应格式(code/msg/data),业务失败时抛出 BusinessError

全局配置

SmartDio.init(SmartConfig(
  options: BaseOptions(baseUrl: 'https://api.example.com'),
  globalPlugins: [
    ResponseParserPlugin(
      keyMap: ResponseKeyMap(codeKey: 'code', msgKey: 'msg', dataKey: 'data'),
      successCodes: [0, '0'],
    ),
  ],
));

单次请求覆盖

// 方式 1:通过 extra 覆盖 keyMap
final response = await SmartDio.get(
  '/api/legacy',
  extra: {
    kResponseKeyMapKey: ResponseKeyMap(codeKey: 'status'),
  },
);

// 方式 2:通过 extraPlugins 完全覆盖
final response = await SmartDio.get(
  '/api/legacy',
  extraPlugins: [
    ResponseParserPlugin(
      keyMap: ResponseKeyMap(codeKey: 'status'),
      successCodes: [200],
    ),
  ],
  excludePlugins: [ResponseParserPlugin],
);
Inheritance

Constructors

ResponseParserPlugin({ResponseKeyMap keyMap = const ResponseKeyMap(), List successCodes = const [0], BusinessSuccessChecker? successChecker, bool skipNonMap = true})

Properties

hashCode int
The hash code for this object.
no setterinherited
keyMap ResponseKeyMap
key 映射配置
final
name String
插件名称(用于调试)
no setteroverride
priority int
优先级 60:Cache(50) < Parser(60) < Retry(80)
no setteroverride
runtimeType Type
A representation of the runtime type of the object.
no setterinherited
skipNonMap bool
是否跳过非 Map 响应,默认 true
final
successChecker BusinessSuccessChecker?
自定义成功判断器(优先于 successCodes)
final
successCodes List
成功码列表,默认 0
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)
inherited
onResponse(RequestContext ctx, Response response) Future<void>
响应接收后调用
override
onStart(RequestContext ctx) Future<void>
请求开始前调用
inherited
toString() String
A string representation of this object.
inherited

Operators

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