RetryExecutor<T> class

失败自动重试执行器

封装重试逻辑,支持指数退避、自定义重试条件、取消等功能。

基础用法

final executor = RetryExecutor<String>(
  config: RetryConfig(maxRetries: 3),
);

try {
  final result = await executor.execute(() async {
    final response = await dio.get('/api/data');
    return response.data;
  });
  print('Success: $result');
} catch (e) {
  print('Failed after retries: $e');
}

带回调的重试

final result = await executor.execute(
  () => fetchData(),
  onRetry: (attempt, error) {
    print('重试第 $attempt 次,原因: $error');
  },
);

支持取消

final cancelToken = CancelToken();

// 在其他地方取消
cancelToken.cancel('User cancelled');

try {
  await executor.execute(
    () => fetchData(),
    cancelToken: cancelToken,
  );
} on RetryCancelledException {
  print('重试被取消');
}

Constructors

RetryExecutor({required RetryConfig config})

Properties

config RetryConfig
重试配置
final
hashCode int
The hash code for this object.
no setterinherited
runtimeType Type
A representation of the runtime type of the object.
no setterinherited

Methods

cancel() → void
取消当前重试
dispose() → void
释放资源
execute(Future<T> action(), {CancelToken? cancelToken, void onRetry(int attempt, dynamic error)?}) Future<T>
执行带重试逻辑的异步函数
noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
toString() String
A string representation of this object.
inherited

Operators

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