RetryPlugin class

重试插件

基于 dio_smart_retry 的重试管理,支持全局和单次请求级别的重试过滤。 懒加载安装 SmartRetryInterceptor 到 Dio 实例。 priority = 80

基本用法

// 使用默认重试逻辑
RetryPlugin(retries: 3)

// 使用 SmartError 模式匹配自定义重试
RetryPlugin(
  retries: 3,
  shouldRetry: (error) => switch (error) {
    NetworkError() => true,
    TimeoutError() => true,
    ResponseError(:final statusCode) => statusCode >= 500,
    _ => false,
  },
)

// 使用默认 shouldRetry
RetryPlugin(shouldRetry: RetryPlugin.defaultShouldRetry)

全局配置

SmartDio.init(SmartConfig(
  globalPlugins: [
    RetryPlugin(
      retries: 3,
      shouldRetry: RetryPlugin.defaultShouldRetry,
    ),
  ],
));

单次请求覆盖

// 仅重试网络错误
SmartDio.get('/api',
  extraPlugins: [
    RetryPlugin(shouldRetry: (e) => e is NetworkError),
  ],
);

// 完全禁用重试
SmartDio.get('/no-retry', excludePlugins: {RetryPlugin});

优先级

  1. retryEvaluator(直接使用 dio_smart_retry 原生接口)
  2. shouldRetry(基于 SmartError,适配为 RetryEvaluator)
  3. dio_smart_retry 默认逻辑(都不传时)
Inheritance

Constructors

RetryPlugin({int retries = 3, List<Duration> retryDelays = const [Duration(seconds: 1), Duration(seconds: 2), Duration(seconds: 3)], ShouldRetry? shouldRetry, RetryEvaluator? retryEvaluator})

Properties

hashCode int
The hash code for this object.
no setterinherited
name String
插件名称(用于调试)
no setteroverride
priority int
优先级,数字越小越先执行,默认 100
no setteroverride
retries int
重试次数
final
retryDelays List<Duration>
重试延迟列表
final
retryEvaluator → RetryEvaluator?
直接使用 dio_smart_retry 的 RetryEvaluator
final
runtimeType Type
A representation of the runtime type of the object.
no setterinherited
shouldRetry ShouldRetry?
基于 SmartError 的重试判断函数
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>
请求开始前调用
override
toString() String
A string representation of this object.
inherited

Operators

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

Static Methods

defaultShouldRetry(SmartError error) bool
默认的重试判断逻辑
resetInstallState() → void
重置安装状态(用于测试)