【发布时间】:2019-02-20 18:14:51
【问题描述】:
我有一个Stream,我从数据库查询中得到。我相信查询会运行一段时间,并在运行时产生值,所以我希望能够在项目可用时立即向用户显示。
但是一旦用户选择了一个项目,我希望 Stream 被取消。
我在写这篇文章时遇到了麻烦,因为我不知道如何才能获得可以在以后取消的流的订阅,同时映射其元素,以便我的映射流的使用者可以处理由原始 Stream 生成的项目。
基本上,我认为我需要CancellableStream 之类的东西,但我在 Dart SDK 中看不到类似的东西。
到目前为止,我已经尝试过这样的事情:
final subscription = cursor.listen((entry) => process(entry));
// now I can cancel the subscription when needed, but how to
// return the processed items to the caller?
final processed = cursor.map((entry) => process(entry));
// now I have the processed Stream I wanted, but how can I cancel it?
【问题讨论】:
标签: asynchronous dart