先贴一下该方法的源码:

  /*** Handles errors emitted by this [Future].** This is the asynchronous equivalent of a "catch" block.** Returns a new [Future] that will be completed with either the result of* this future or the result of calling the `onError` callback.** If this future completes with a value,* the returned future completes with the same value.** If this future completes with an error,* then [test] is first called with the error value.** If `test` returns false, the exception is not handled by this `catchError`,* and the returned future completes with the same error and stack trace* as this future.** If `test` returns `true`,* [onError] is called with the error and possibly stack trace,* and the returned future is completed with the result of this call* in exactly the same way as for [then]'s `onError`.** If `test` is omitted, it defaults to a function that always returns true.* The `test` function should not throw, but if it does, it is handled as* if the `onError` function had thrown.** Note that futures don't delay reporting of errors until listeners are* added. If the first `catchError` (or `then`) call happens after this future* has completed with an error then the error is reported as unhandled error.* See the description on [Future].*/// The `Function` below stands for one of two types:// - (dynamic) -> FutureOr<T>// - (dynamic, StackTrace) -> FutureOr<T>// Given that there is a `test` function that is usually used to do an// `isCheck` we should also expect functions that take a specific argument.// Note: making `catchError` return a `Future<T>` in non-strong mode could be// a breaking change.Future<T> catchError(Function onError, {bool test(Object error)});
复制代码

翻译一下主要的意思,就是这个catchError方法可以捕获其他Futrue的异常信息,如果重写了test方法,test返回true就可以在catchError的onError方法里捕获到异常,如果test返回false,就把该异常继续抛出而不会在catchError方法里被捕获,如果不写test默认实现一个返回true的test方法,注意catchError只能捕获Future的异常,而不能捕获同步代码的异常,测试代码如下:

import 'dart:async';Future testFutureError() {return new Future(() {throw "error";//1
//    return "abc";//2});
}main() {testFutureError().then((value) {print("then " + value);}).catchError((e) {print("catchError " + e);}, test: (Object o) {print("test " + o);return true;//3
//    return false;//4}).catchError((e) {print("catchError2 " + e);}, test: (_) => true);
}
复制代码

如果是上面的代码,会输出

test error
catchError error
复制代码

因为//3这行返回了true,所以会在第一个catchError里被捕获。 如果//4不注释了,把//3注释,那么第一个catchError不能捕获该异常,该异常会继续抛出,然后在第二个catchError里被捕获。具体的可自行测试。

还有就是用whenComplete方法的时候,不是所有then都执行完再执行whenComplete方法,而是then、catchError、whenComplete这些方法会按照顺序(除非中途有异常会进入下一个catchError)执行,这一点和rxjava+retrofit不一样,原生开发请知悉。

Dart中的catchError捕获顺序相关推荐

  1. 正则表达式中的非捕获组是什么?

    非捕获组(即(?:) )如何在正则表达式中使用,它们有什么用? #1楼 在复杂的正则表达式中,您可能会希望使用大量的组,其中一些用于重复匹配,而另一些则提供反向引用. 默认情况下,与每个组匹配的文本会 ...

  2. 详解Dart中如何通过注解生成代码

    简介:详解dart与java注解生成代码异同点 作者:闲鱼技术-龙湫 1.背景 最近在项目中使用到了Dart中的注解代码生成技术,这跟之前Java中APT+JavaPoet生成代码那套技术还是有一些不 ...

  3. dart系列之:dart中的异步编程

    文章目录 简介 为什么要用异步编程 怎么使用 Future 异步异常处理 在同步函数中调用异步函数 总结 简介 熟悉javascript的朋友应该知道,在ES6中引入了await和async的语法,可 ...

  4. Dart中Map的使用

    本篇文章总结了Dart中map的使用方式,由于map中的函数相对较少,没有做明确的顺序,代码如下: void main(List<String> args) {print("这个 ...

  5. 大数据——Java中异常的捕获和处理

    Java中异常的捕获和处理 异常的概述 认识异常 Java异常体系结构 Java异常处理机制 异常处理 使用try-catch处理异常 使用try-catch-finally处理异常 使用多重catc ...

  6. js中冒泡和捕获/阻止冒泡和捕获事件

    JavaScript中捕获/阻止捕获.冒泡/阻止冒泡 事件流描述的是从页面中接收事件的顺序.提出事件流概念的正是IE和Netscape,但是前者提出的是我们常用的事件冒泡流,而后者提出的是事件捕获流. ...

  7. java异常在哪一层捕获_当在一个方法的代码中抛出一个检测异常时,该异常或被方法中的 ( )结构 捕获,或者在方法的 ( ) 中声明_学小易找答案...

    [填空题]当异常已经被定义时,必须通过( ) 语句来处理它. [填空题]Catch 子句包含( )的程序段 [单选题]下列java语言的常用异常类中,属于检测异常的是() [单选题]自定义异常类时,可 ...

  8. Java中对象的实例化顺序

    文章目录 1 Java中对象的实例化顺序 1 Java中对象的实例化顺序 继承后的初始化顺序: 静态成员包括静态构造代码块,初始化顺序跟书写顺序有关.

  9. 如何在Dart中读取控制台输入/ stdin?

    如何在Dart中读取控制台输入/ stdin? import 'dart:io';void main(){stdout.write("请输入: ");var name = stdi ...

最新文章

  1. redis删除过期key的算法_面试官别再问我Redis内存满了该怎么办了
  2. ISP PIPLINE (九_2) Denoise 之 time domain denoise
  3. codevs 2606 约数和(分块优化数学公式 )
  4. Dockerfile镜像的制作
  5. Java程序员需要掌握的计算机底层知识(三):进程、线程、纤程、中断
  6. 昆士兰科技大学计算机专业,昆士兰科技大学QUT计算机科学Computer Science专业排名第101-125位(2021年THE世界大学商科排名)...
  7. 关于向MySQL插入一条新纪录的问题
  8. 基于selenium的爬虫
  9. 新浪微博放开140字限制:社交向左 原创向右
  10. 被孟加拉题吊打的ACM考试
  11. 计算机课程设计心得体会及总结,课程设计心得体会
  12. php js条形码扫描,使用JavaScript根据图片获取条形码的方法
  13. 转载:医疗保险,公积金、养老、生育、工伤、失业保险
  14. 什么是Java api
  15. 第十五届东北大学生编程大赛题解
  16. Android系统字体加载流程
  17. matlab异步电机仿真,基于MATLAB的异步电机仿真系统
  18. 关于“无法读取 KSR data-disk 1 扇区。。”蓝屏问题
  19. cps1 cps2 android,CPS1和CPS2模拟器详细图文教程
  20. 数据库结构比对,再初始数据比对方法

热门文章

  1. php中的eq的含义,jquery,_jQuery中的eq(0)到底是什么意思??详情请看下面代码!,jquery - phpStudy...
  2. leetcode算法题--球会落何处
  3. fs_struct和file_struct关系
  4. leetcode算法题--数组中两个数的最大异或值
  5. leetcode算法题--分裂二叉树的最大乘积
  6. chrome调试的JavaScript官方技巧
  7. 第二十二章:动画(十五)
  8. 通过几个例子看sed的模式空间与保持空间
  9. linux 查看端口被哪个程序占用
  10. [导入]ASP.Net环境下使用Jmail组件发送邮件