搜索插件、代码仓与 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 项

源码仓库

使用此插件的项目

暂无相关数据。