【发布时间】:2021-06-06 22:40:24
【问题描述】:
我正在将 dart 代码转换为 nnbd。
我有以下代码。
var subscription = response.listen(
(newBytes) async {
/// if we don't pause we get overlapping calls from listen
/// which causes the [writeFrom] to fail as you can't
/// do overlapping io.
subscription.pause();
/// we have new data to save.
await raf.writeFrom(newBytes);
subscription.resume();
});
问题是我收到以下错误:
The non-nullable local variable 'subscription' must be assigned before it can be used.
Try giving it an initializer expression, or ensure that it's assigned on every execution path.
我在这里解决了类似的问题: dart - correct coding pattern for subscription when using null saftey? @lrn 回答了这个问题
但是,模式解决方案模式在这种情况下似乎不起作用。
raf.writeFrom 是一个异步操作,因此我必须使用“异步”方法,这意味着我不能再使用“forEach”解决方案,因为我无权访问订阅对象。
【问题讨论】:
标签: dart subscription