解决 Flutter Dio合并请求多个接口,如果一个接口500,那么导致其他请求不在执行

Flutter Dio如何自定义拦截异常

应用场景

我们一般会统一拦截DioExceptionType 如400,403,500 等错误

但有时候,有个地方合并请求多个接口,如果一个接口500,那么导致其他请求不在执行,因为统一拦截了500,

修改方法在请求时加一个Header 如:isIntercep 字段,在Dio onError 时 解出这个Header来,如有这个字段返回如下,就是不拦截,这样的话只是这个接口返回数据错误,不影响其他接口执行

dart 复制代码
 handler.resolve(
        Response<Map<String, dynamic>>(
          requestOptions: err.requestOptions,
          statusCode: 200,
          data: {},
        ),

///请求公共参数拦截器
class HttpParamsInterceptor extends Interceptor {
  static const language = "language";
  static const appname = "appname";
  static const mobileType = "mobileType";
  static const mobileId = "mobileId";
  static const version = "version";

  @override
  void onRequest(RequestOptions options, RequestInterceptorHandler handler) {
    var headers = options.headers;
    headers[language] = LocaleController.getCountryCode;
    headers[mobileType] = GetPlatform.isAndroid ? "Android" : "IOS";
    headers[version] = PackageInfoManager().version;
    headers[appname] = PackageInfoManager().appName;
    if (AppConfig.instance.enableRequestJsonLog) {
      Log.r(
          '${"--------------------onRequest---------------"}\n${options.method}${' Url:'} ${options.uri}\n${'Query Parameters:'}${options.queryParameters}\n${'Body:'}${options.data != null ? const JsonEncoder.withIndent('  ').convert(options.data) : ""}\n\n');
    }
    super.onRequest(options, handler);
  }

  @override
  void onResponse(Response response, ResponseInterceptorHandler handler) {
    if (AppConfig.instance.enableResponseJsonLog) {
      Log.r(
          '${"-----------------------------Response Start-----------------------------"}\n${response.requestOptions.method}${' Url:'} ${response.requestOptions.uri}\n${'Query Parameters:'}${response.requestOptions.queryParameters}\n${'Body:'}${response.requestOptions.data != null ? const JsonEncoder.withIndent('  ').convert(response.requestOptions.data) : ""}\n${'response data:'}\n$response\n\n${"-----------------------------Response End-------------------------------"}\n\n\n');
    }
    super.onResponse(response, handler);
  }

  @override
  Future<void> onError(
      DioException err, ErrorInterceptorHandler handler) async {
    AppException appException = AppException.create(err);
    if (AppConfig.instance.enableRequestLog) {
      Log.e('DioException===: +${err.toString()}');
    }
    Log.logPrint(err.requestOptions.headers);
    Log.logPrint(appException.type);
    Map<String, dynamic> data = err.requestOptions.headers;
    bool? isIntercep = data[APIConstant.ignoreIntercep];
    if (isIntercep == true &&
        (appException.type == DioExceptionType.badResponse ||
            appException.type == DioExceptionType.unknown)) {
      return handler.resolve(
        Response<Map<String, dynamic>>(
          requestOptions: err.requestOptions,
          statusCode: 200,
          data: {},
        ),
      );
    }

    return handler.next(appException);
  }
}
相关推荐
jcLee9520 小时前
Flutter/Dart:使用日志模块Logger Easier
flutter·log4j·dart·logger
tmacfrank21 小时前
Flutter 异步编程简述
flutter
tmacfrank21 小时前
Flutter 基础知识总结
flutter
叫我菜菜就好1 天前
【Flutter_Web】Flutter编译Web第三篇(网络请求篇):dio如何改造方法,变成web之后数据如何处理
前端·网络·flutter
AiFlutter1 天前
Flutter-底部分享弹窗(showModalBottomSheet)
java·前端·flutter
m0_748247802 天前
Flutter Intl包使用指南:实现国际化和本地化
前端·javascript·flutter
迷雾漫步者2 天前
Flutter组件————PageView
flutter·跨平台·dart
迷雾漫步者2 天前
Flutter组件————FloatingActionButton
前端·flutter·dart
coder_pig3 天前
📝小记:Ubuntu 部署 Jenkins 打包 Flutter APK
flutter·ubuntu·jenkins
捡芝麻丢西瓜3 天前
flutter自学笔记5- dart 编码规范
flutter·dart