搜尋套件、儲存庫與 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 項

原始碼儲存庫

使用此套件的專案