Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Filter data from Stream in Flutter/Dart

Tags:

flutter

dart

I have a Stream which contains List of objects. i.e. List<ProductSubCategoryListModel> and ProductSubCategoryListModel contains isSelected boolean and CategoryId int.

I want to filter Stream which only has isSelected == true and get list type of List<ProductSubCategoryListModel> in return. After this need to get all the CategoryId from `List.

Here's my code.

  Future<void> fetchProducts(
  TrendingCategoriesModel categoryModel, int pageIndex) async {
_localCategoryModel = categoryModel;

//Stream I want to manipulate to get List<ProductSubCategoryListModel>
var stream = _categoryListController.stream;

var request = ProductListRequestModel();
var searchParameterModel = SearchParams();
searchParameterModel.SearchText = '';
searchParameterModel.CategoryIDs = //List<int> of CategoryIds filtered from List<ProductSubCategoryListModel>;
searchParameterModel.SubCategoryIDs = null;
searchParameterModel.AttributeIDs = null;
searchParameterModel.SubAttributeIDs = null;
searchParameterModel.StartPrice = 0;
searchParameterModel.EndPrice = null;
searchParameterModel.EndPrice = null;

request.PageIndex = pageIndex;
request.PageSize = 10;
request.SortExpression = '';
request.SortType = '';
request.IsGetFilterData = true;
request.searchParams = searchParameterModel;

if (!_productListController.isClosed) _productListController.sink.add(null);
var response =
    await _networkHandler.callAPI(NetworkUrl.PRODUCT_LIST, request);
if (response != null) {
}

}

like image 896
Salman Shaikh Avatar asked Dec 02 '25 10:12

Salman Shaikh


1 Answers

You can use where method of stream which will return only whose elements that has isSelected flag true.

var stream = _categoryListController.stream;

  stream = stream.where((element) {
    return element
        .isSelected;
  }).toList();
like image 117
naman kashyap Avatar answered Dec 04 '25 02:12

naman kashyap



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!