搜尋套件、儲存庫與 APP 分類

套件詳情v2.0.8

dio_retry_plus

一個用於 dio 的外掛程式,可重試失敗的請求。

AndroidiOSWebmacOSWindowsLinux
近 30 天下載
837
喜歡數
29
Pub 分數
120

安裝套件

flutter pub add dio_retry_plus

套件介紹

根據套件公開資料產生的選型評估,僅供參考。

dio_retry_plus 是一個擴展 Dio(一個流行的 HTTP 客戶端)的 Flutter 套件,為失敗的請求添加自動重試功能。它透過基於可設定條件(例如錯誤類型、狀態碼或逾時)重試請求,幫助提升網路操作的可靠性。該套件支援多種重試策略與指數退避機制,非常適合需要強大網路韌性的應用程式。它能無縫整合至現有的 Dio 設定中,並在 Android、iOS、Web、macOS、Windows 和 Linux 等所有主要平台運作。

適用情境

  • 處理暫時性網路失敗
  • 提高 API 呼叫的可靠性
  • 減少使用者因請求失敗而產生的挫折感
  • 支援離線優先的應用模式
  • 自動化後台任務的重試邏輯

主要特色

  • 可設定的重試條件
  • 指數退避支援
  • 重試次數自定義
  • 與 Dio 中間件整合
  • 逾時或連接錯誤時自動重試

適合對象

  • 網路連接不穩定的應用程式
  • 需要容錯的 API 客戶端
  • 背景資料同步服務
  • 即時數據獲取應用
  • 使用 Dio 進行 HTTP 操作的開發者

使用建議

  • 如果重試過於頻繁,可能會增加延遲
  • 需要仔細設定以避免無限循環
  • 不適用於僅冪等操作
  • 可能影響移動設備的電池壽命
  • 需要在模擬網路故障場景下進行測試

原始 README

以下為關聯原始碼儲存庫的 README 原文快照,最新內容請造訪原始碼儲存庫。

dio_retry_plus

A plugin for dio that retries failed requests.

Usage

import 'package:dio_retry_plus/dio_retry_plus.dart';
Basic configuration
final dio = Dio()
  ..interceptors.add(RetryInterceptor());
Global retry options
final dio = Dio()
  ..interceptors.add(RetryInterceptor(
    options: const RetryOptions(
      retries: 3, // Number of retries before a failure
      retryInterval: const Duration(seconds: 1), // Interval between each retry
      retryEvaluator: (error) => error.type != DioErrorType.CANCEL && error.type != DioErrorType.RESPONSE, // Evaluating if a retry is necessary regarding the error. It is a good candidate for updating authentication token in case of a unauthorized error (be careful with concurrency though)
    )
  )
);
Sending a request with options
final response = await dio.get("http://www.flutter.dev", options: Options(
    extra: RetryOptions(
      retryInterval: const Duration(seconds: 10),
    ).toExtra(),
  ));
Sending a request without retry
final response = await dio.get("http://www.flutter.dev", options: Options(
    extra: RetryOptions.noRetry().toExtra(),
  ));
Logging retry operations
final dio = Dio()
  ..interceptors.add(RetryInterceptor(logger: Logger("Retry")));

Features and bugs

Please file issues.

所屬分類

依賴

依賴清單 1 項

原始碼儲存庫

使用此套件的專案

暫無相關資料。