0
我正在使用dart:async
庫來執行一些數據處理。我將對象添加到StreamController
,另一個模塊正在監聽流。現在我想,其他模塊正在將處理結果返回給add
呼叫(作爲未來)。帶返回值的事件
下面是一些例子代碼,應該說明我想做的事(這是不行的,因爲add方法不返回未來):
final controller = new StreamController();
controller.stream.listen((a) {
// Do something with a, after that return something:
return 42;
});
final aFuture = controller.add(new A());
aFuture.then((result) {
// result == 42
});
是像這可能與dart:async
,另一個圖書館,還是我需要編寫自己的課程?
PS:另一種方法是以下,但是這將是「更復雜」用一個簡單的返回:
final controller = new StreamController();
controller.stream.listen((container) {
// Do something with container.a, after that return something:
container.completer.complete(42);
});
final completer = new Completer();
controller.add(new Container(new A(), completer));
completer.future.then((result) {
// result == 42
});
這類似於我想到了第二種方式,但沒有我在找什麼... – Fox32