搜索插件、代码仓与 APP 分类

插件详情v7.0.1

dio_smart_retry

专为Dio和Dio包打造的重试库,饱含爱意。默认情况下,仅对合适的可重试HTTP状态码进行请求重试。

AndroidiOSWebmacOSWindowsLinux
近 30 天下载
20.9万
喜欢数
314
Pub 分数
145

安装插件

flutter pub add dio_smart_retry

插件介绍

基于插件公开信息、社区数据与项目使用情况整理的选型参考。

Dio Smart Retry 是一个为 Dio HTTP 客户端设计的重试工具,可根据预定义条件自动重试请求。它默认仅对被视为可重试的 HTTP 状态码进行重试,例如 5xx 服务器错误或网络超时。该工具以简单性和可靠性为核心设计,可无缝集成到现有的 Dio 配置中。该包支持所有主要平台,包括 Android、iOS、Web、macOS、Windows 和 Linux,非常适合在跨平台 Flutter 应用中实现稳健的网络操作。

适合场景

  • 处理瞬时网络故障
  • 自动重试服务器错误
  • 在连接状况不佳时提升应用程序的容错能力
  • 简化 API 调用中的错误处理
  • 减少业务代码中的手动重试逻辑

主要特点

  • 自动重试可重试的HTTP状态
  • 与 Dio 的轻松集成
  • 支持多个平台
  • 可配置的重试行为
  • 轻量且开销极小

适合对象

  • 频繁调用 API 的 Flutter 应用
  • 需要可靠网络通信的应用程序
  • 寻求简单重试逻辑的开发者
  • 使用 Dio 进行 HTTP 请求的项目
  • 希望减少样板错误处理的团队

使用建议

  • 默认情况下,不会重试客户端错误(4xx)
  • 自定义重试逻辑需要额外配置
  • 仅限基于 Dio 的 HTTP 客户端
  • 可能需要针对高延迟环境进行调整
  • 没有内置的退避策略,仅支持基本重试

原始 README

以下为关联源码仓库的 README 原文快照,最新内容请访问源码仓库。

Pub Version Dart SDK Version style: very good analysis License

Dio Smart Retry

Flexible retry library for Dio package. This is a next generation of an abandoned dio_retry package.
By default, the request will be retried only for appropriate retryable http statuses.
Also, it supports dynamic delay between retries.
Null Safety.

Contents

Getting Started for Dio

  1. Add package to pubspec.yaml: dio_smart_retry: ^7.0.0 **
  2. Import package: import 'package:dio_smart_retry/dio_smart_retry.dart'

** For the old dio (ver. 4.+) use dio_smart_retry: ^1.4.0

Usage

Just add an interceptor to your dio:

final dio = Dio();
// Add the interceptor
dio.interceptors.add(RetryInterceptor(
  dio: dio,
  logPrint: print, // specify log function (optional)
  retries: 3, // retry count (optional)
  retryDelays: const [ // set delays between retries (optional)
    Duration(seconds: 1), // wait 1 sec before first retry
    Duration(seconds: 2), // wait 2 sec before second retry
    Duration(seconds: 3), // wait 3 sec before third retry
  ],
));

/// Sending a failing request for 3 times with 1s, then 2s, then 3s interval
await dio.get('https://mock.codes/500');

See example/dio_smart_retry_example.dart.

Default retryable status codes list

Responses with these http status codes will be retried by default:

  • 408: RequestTimeout
  • 429: TooManyRequests
  • 500: InternalServerError
  • 502: BadGateway
  • 503: ServiceUnavailable
  • 504: GatewayTimeout
  • 440: LoginTimeout (IIS)
  • 460: ClientClosedRequest (AWS Elastic Load Balancer)
  • 499: ClientClosedRequest (ngnix)
  • 520: WebServerReturnedUnknownError
  • 521: WebServerIsDown
  • 522: ConnectionTimedOut
  • 523: OriginIsUnreachable
  • 524: TimeoutOccurred
  • 525: SSLHandshakeFailed
  • 527: RailgunError
  • 598: NetworkReadTimeoutError
  • 599: NetworkConnectTimeoutError It's possible to override this list

Disable retry

It's possible to manually disable retry for a specified request. Use disableRetry extension for that:

final request = RequestOptions(path: '/')
  ..disableRetry = true;
await dio.fetch<String>(request);

or

final options = Options()
  ..disableRetry = true;
await dio.get<String>('/', options: options);

Add extra retryable status codes

It's possible to add you own retryable status codes. Use retryableExtraStatuses parameter for that. Here is an example:

RetryInterceptor(
  dio: dio,
  retryableExtraStatuses: { status401Unauthorized },
)

or:

RetryInterceptor(
  dio: dio,
  retryableExtraStatuses: { 401 },
)

Override retryable statuses

It's possible to override default retryable status codes list. Just make new instance of DefaultRetryEvaluator and pass your status codes there.
Here is an example:

final myStatuses = { status400BadRequest, status409Conflict };
dio.interceptors.add(
  RetryInterceptor(
    dio: dio,
    logPrint: print,
    retryEvaluator: DefaultRetryEvaluator(myStatuses).evaluate,
  ),
);

await dio.get<dynamic>('https://mock.codes/400');

Migrating to 7.0

Use MultipartFile instead of MultipartFileRecreatable.

所属分类

依赖

依赖清单 3 项

源码仓库

使用此插件的项目