runZoned提供的特殊功能dart:async.该文件在此处:https://api.dartlang.org/docs/channels/stable/latest/dart_async.html#runZoned
我不确定这个功能的目的是什么,我们什么时候需要它,以及如何正确使用它?
看看这段代码:
import 'dart:async';
void main() {
fineMethod().catchError((s) {}, test : (e) => e is String);
badMethod().catchError((s) {}, test : (e) => e is String);
}
Future fineMethod() {
return new Future(() => throw "I am fine");
}
Future badMethod() {
new Future(() => throw "I am bad");
return new Future(() => throw "I am fine");
}
Run Code Online (Sandbox Code Playgroud)
产量
Unhandled exception:
I am bad
Run Code Online (Sandbox Code Playgroud)
现在看看这段代码:
import 'dart:async';
void main() {
fineMethod().catchError((s) {}, test : (e) => e is String);
runZoned(() {
badMethod().catchError((s) {}, test : (e) => e is String);
}, onError : (s) {
print("It's not so bad but good in this also not so big.");
print("Problem still exists: $s");
});
}
Future fineMethod() {
return new Future(() => throw "I am fine");
}
Future badMethod() {
new Future(() => throw "I am bad");
return new Future(() => throw "I am fine");
}
Run Code Online (Sandbox Code Playgroud)
产量
It's not so bad but good in this also not so big.
Problem still exists: I am bad
Run Code Online (Sandbox Code Playgroud)
badMethod如果可能的话,你应该严格避免使用.
只有在不可能的情况下,您才可以临时使用 runZoned
您也可以使用runZoned模拟sandboxed任务的执行.
| 归档时间: |
|
| 查看次数: |
486 次 |
| 最近记录: |