dart - 如何过滤 rxdart 中的 obseravble 列表
问题描述
我正在尝试在 rxdart 中实现 bloc 模式。我正在尝试构建 app 类型的 todo 应用程序。我实现了显示列表中的所有项目,但我想要的不是在不同部分显示已完成和未完成的项目。但是,我无法根据完成的 rxdart 过滤项目。
import 'package:rxdart/rxdart.dart';
import '../models/ShoppingItem.dart';
class ShoppingItemBloc {
final _shoppingItems = BehaviorSubject<List<ShoppingItem>>
(seedValue: []);
Observable<List<ShoppingItem>> get allShoppingItems =>
_shoppingItems.stream;
//Getter to implement
Observable<List<ShoppingItem>> get completedShoppingItems =>
dispose() {
_shoppingItems.close();
}
}
我想要的是获得完成的 shoppingItems 。ShoppingItem 类有一个布尔属性 completed 。我想在此基础上对其进行过滤。
任何帮助将不胜感激
解决方案
您可以根据需要使用流中的位置进行过滤。由于您正在观察项目列表,因此您需要在过滤单个项目之前进行映射。在我们的例子中,它会是这样的。
Observable<List<ShoppingItem>> get completedShoppingItems =>
_shoppingItems.stream.map((itemList) =>
itemList.where((item) => item.completed));
推荐阅读
- python - 当我们在循环中插入新行时,如何遍历电子表格中的所有行?
- c# - 在 GridView 中删除行时出错
- android - 相机点击 Xamarin Force Portrait
- python - 过滤具有精确字符串匹配的数据框
- nuxt.js - 如何以与@nuxt/apollo subscribeToMore 一起使用的方式设置我的 nuxt
- typescript - 使用 Jest/Supertest 进行 Express/Typescript 测试
- java - 使用 Random 播种时 SecureRandom 会减弱吗?
- c++ - 我可以从使用 c++ 和 windows 连接到服务器的套接字中获得什么信息?
- excel - 需要帮助编写 IF 语句
- docusignapi - Docusign 一键登录