在 flutter 中,我们使用 StreamBuilder 和接收流 e 给我们一个包含“数据”但也包含“错误”对象的快照对象。
我想用 async* 制作一个产生数据的函数,但由于某些条件,它也会产生一些错误。我怎样才能在 Dart 中实现这一目标?
Stream<int> justAFunction() async* {
yield 0;
for (var i = 1; i < 11; ++i) {
await Future.delayed(millis(500));
yield i;
}
yield AnyError(); <- I WANT TO YIELD THIS!
}
Run Code Online (Sandbox Code Playgroud)
然后,在 StreamBuilder 中:
StreamBuilder(
stream: justAFunction(),
builder: (BuildContext context, AsyncSnapshot<RequestResult> snapshot) {
return Center(child: Text("The error tha came: ${snapshot.error}")); <- THIS SHOULD BE THE AnyError ABOVE!
},
)
Run Code Online (Sandbox Code Playgroud)
Rém*_*let 10
简单地扔
Stream<int> foo() async* {
throw FormatException();
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
767 次 |
| 最近记录: |