如何在rxdart中过滤可疑列表 [英] How to filter a list of obseravble in rxdart

查看:95
本文介绍了如何在rxdart中过滤可疑列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在rxdart中实现bloc模式.我正在尝试构建app的todo app类型.我实现了显示列表中的所有项目,但是我不希望不在不同部分显示已完成和未完成的项目.但是我无法根据rxdart上的完成内容过滤项目.

I am trying to implement bloc pattern in rxdart . I am trying to build todo app type of app . I implemented showing all items in list but what I want is not to show completed and uncompleted items in different part . However I am not able to filter the items based on completed on 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类的布尔属性已完成.我想在此基础上对其进行过滤.

What I wanted is to get the completed shoppingItems . The class ShoppingItem has a boolean property completed . I wanted to filter it on that basis .

任何帮助都将感谢

推荐答案

,您可以根据需要使用流中的where过滤掉.由于您正在观察项目列表,因此在过滤单个项目之前需要先进行映射.就我们而言,就是这样.

you can use where on the stream to filter out as per your need. Since you are observing list of item you need to map before filtering individual item. In our case it would be something like this.

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

这篇关于如何在rxdart中过滤可疑列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆