我正在使用包含Products
和Categories
的流构建一些逻辑。 Product
具有id
,而另一项Category
具有类型product
的{{1}}属性,其中字符串代表产品的List<String>
。
我将两个流组合在一起,一个代表产品,另一个代表选定的类别,它将产生另一个与选定类别有关的已过滤产品的流。
id
命令方法(forEach,向列表中添加内容)是正确的。当我在列表上使用import 'package:rxdart/rxdart.dart';
class ProductsScreenBloc {
Observable<List<dynamic>> _products;
Observable<List<dynamic>> _categories;
final _selectedCategory = BehaviorSubject<dynamic>.seeded(null);
ProductsScreenBloc({products, categories}) {
_products = products;
_categories = categories;
}
Observable<List<dynamic>> get products => _products;
Observable<List<dynamic>> get productsFiltered =>
Observable.combineLatest2(_products, selectedCategory,
(products, category) {
if (category == null) {
return products;
}
print('click');
final availableIds = category['products'];
// final items = products
// .where((item) => availableIds.contains(item["name"]))
// .toList();
final List<dynamic> items = [];
products.forEach((product) {
if (availableIds.contains(product["id"])) {
items.add(product);
}
});
print(items);
print('items filtered');
return items;
});
Observable<List<dynamic>> get categories => _categories;
Observable<dynamic> get selectedCategory => _selectedCategory.stream;
Function(dynamic) get selectCategory => _selectedCategory.sink.add;
dispose() {
_selectedCategory.close();
}
}
方法时,会出现一些偷偷摸摸的错误或类似的错误,它甚至不会在控制台上打印任何内容,并且也不执行每次打印,返回,函数调用等...完全
控制台输出示例(where方法)
where
控制台输出示例(foreach)
flutter: click
flutter: null -- this is the value that is returned from the productsFiltered
我在dartpad中编写了一些代码示例,两种方法都可以正常使用__(ツ)_ /¯
flutter: click
flutter: [array of filtered items]
flutter: items filtered
flutter: [array of filtered items] - return value from the productsFiltered