首页 > 解决方案 > 如何过滤 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 。我想在此基础上对其进行过滤。

任何帮助将不胜感激

标签: dartflutterreactive-programmingrxdartbloc

解决方案


您可以根据需要使用流中的位置进行过滤。由于您正在观察项目列表,因此您需要在过滤单个项目之前进行映射。在我们的例子中,它会是这样的。

 Observable<List<ShoppingItem>> get completedShoppingItems => 
    _shoppingItems.stream.map((itemList) =>
        itemList.where((item) => item.completed));

推荐阅读