【问题标题】:How to filter a list of obseravble in rxdart如何过滤 rxdart 中的 observable 列表
【发布时间】:2019-06-27 06:12:59
【问题描述】:

我正在尝试在 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 。我想在此基础上对其进行过滤。

任何帮助将不胜感激

【问题讨论】:

    标签: dart flutter reactive-programming rxdart bloc


    【解决方案1】:

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

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

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-12-03
    • 1970-01-01
    • 2020-01-09
    • 1970-01-01
    • 2020-04-07
    • 2018-05-25
    • 2015-08-27
    • 2018-06-27
    相关资源
    最近更新 更多