我一直在试图找到一种很好的方法来做到这一点,但我没有运气。
这是问题的简化版本:
import 'package:rxdart/rxdart.dart';
/// Input a list of integers [0,1,2,3,4]
/// map each of those integers to the corresponding index in the map
/// if the map updates, the output should update too.
///
/// The output should be a list of Strings:
/// ["Hi from 1", "Hi from 2"; "Hi from 3", "Hi from 4", "Hi from 5"]
BehaviorSubject<Map<int, String>> subject = BehaviorSubject(
seedValue: {
1: "Hi from 1",
2: "Hi from 2",
3: "Hi from 3",
4: "Hi from 4",
5: "Hi from 5",
}
);
void main() {
Observable.fromIterable([1, 2, 3, 4, 5])
.flatMap((index) => subject.stream.map((map) => map[index]))
.toList().asObservable()
.listen((data) {
print("List of data incoming $data");
});
}
运行此程序时,不会打印任何内容。这是因为主题永远不会完成,因此toList()
永远不会完成构建列表。
用例如 an 替换主题Observable.just(index + 2)
确实有效,因为 Observable 完成并且toList()
能够收集它们。
但是预期的行为是,每次更改主题时,该示例都应发出新的字符串列表。
任何帮助,将不胜感激,
谢谢!