最近在做Flutter网络这块,涉及到耗时 *** 作,显然要用异步编程。在其他语言,比如java、c++等,会使用多线程来实现。而在Dart语言中,它是单线程+事件循环的设计模式,其中主要使用Future来实现异步 *** 作,为了快速的了解Flutter的异步编程,需要熟练Futter的概念以及用法。
二、Future是什么同步编程中,立即返回执行的结果或者抛出异常;而异步编程中,返回的是将要完成的结果未来。Future代表异步执行的未来。
var future = Future
上述代码在执行过程中,future是Future的一个实例,有两种状态,完成或者未完成。当调用异步方法的时候,返回的一个未完成的future对象,该对象等待方法中相关 *** 作执行完成或者抛出异常。
void main() { print('start......'); var future = getNetworkData(); print(future); print('do other......'); } FuturegetNetworkData() { return Future(() { //执行其他耗时 *** 作 int result = 0; for (int i = 0; i < 100000; i++) { result += i; } return "result:$result"; }); }
打印结果:
start...... Instance of 'Future2.1、Future如何取值' do other......
Future返回值:
Future 是一个泛型,其中T代表的是我们耗时 *** 作返回的具体值,如果不需要返回值,可以使用Future。
获取耗时 *** 作的返回值:
在Future中通过then回调方式取值:
void main() { print('start......'); var future = getNetworkData(); future.then((value) => print(value)); print(future); print('do other......'); }
执行结果:
start...... Instance of 'Future2.2、Future处理异常' do other...... result:4999950000
异常处理使我们在开发中特别需要注意的,正确的处理程序运行中的异常,能给用户带来更好的体验。
在future中可以使用catchError()或在then()方法中传入可选参数onError,如下所示:
var future = getNetworkData(); future.then((value) { print(value); }, onError: (e) { print(e); }); //或者在catchError中处理异常 future.then((value) { print(value); }).catchError((e) { print(e); }); FuturegetNetworkData() { return Future.delayed(const Duration(seconds: 3), () { int result = 0; for (int i = 0; i < 100000; i++) { result += i; } //抛出异常 return throw Exception('this is '); }); }
try/catch能够捕获future中的异常吗?
try{ var future = getNetworkData(); future.then((value) { print(value); }); }catch(e){ print(e); }
运行上面的代码,控制台会输出以下信息:
Unhandled exception: Exception: this is #0 getNetworkData. (package:demo/test/test_async.dart:23:12) #1 new Future.delayed. (dart:async/future.dart:393:39) #2 Timer._createTimer. (dart:async-patch/timer_patch.dart:18:15) #3 _Timer._runTimers (dart:isolate-patch/timer_impl.dart:395:19) #4 _Timer._handleMessage (dart:isolate-patch/timer_impl.dart:426:5) #5 _RawReceivePortImpl._handleMessage (dart:isolate-patch/isolate_patch.dart:192:12)
根据提示信息,try/catch未能够捕获future中的异常,因此future中的异常只能通过catchError()或在then()方法中传入可选参数onError来进行捕获和处理。
三、使用async和await关键字来处理future作用:
- async:在方法体前面是使用,定义该方法为一个异步方法。await:等待并获得异步表达式的执行结果,并且给关键字只能在async修饰的方法中。
使用demo如下:
Future3.1、处理async方法中的异常printOrderMessage() async{ print('Awaiting user order...'); var order = await fetchUserOrder(); print('You order is:$order'); } Future fetchUserOrder(){ return Future.delayed(const Duration(seconds: 2),(){ return 'Large Latte'; }); }
对于async中的方法的异常,我们按一下方式进行处理:
FutureprintOrderMessage() async{ print('Awaiting user order...'); try{ var order = await fetchUserOrder(); print('You order is:$order'); }catch (e){ print(e); } } Future fetchUserOrder(){ return Future.delayed(const Duration(seconds: 2),(){ return throw Exception('handle order error'); }); }
对于async方法中的异常,需要通过try/catch进行处理。
小结- Future是什么:将要完成的结果未来。Future如何获取异步的值:通过then()方法Future如何处理异常:通过catchError()或者在then()中传入命名参数onErrorasync和wait的关键的作用是什么:async声明一个异步方法,wait等待异步任务完成;简单来讲就是同步的方式编写异步代码。如何捕获和处理async中的异常:需要使用try/catch来捕获以及处理程序运行中的异常。
最后,在初次接触Flutter、asnyc和await时,一定要深入了解其中用法以及处理方式,这样才能在后期的开发中得心应手。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)