Search code examples
flutterdartin-app-purchase

Dart remove item form list if value equal other value


Hi i have list List<ProductDetails> like that i want remove item if price value equal others

1 Gold 1$ 5 Gold 1$ 10 Gold 1$

ProductDetails(
      {@required this.id,
      @required this.title,
      @required this.description,
      @required this.price,
      this.skProduct,
      this.skuDetail});

I want to remove 1 gold and 5 gold just show 10 gold how can i do it ?

Im sorted list like that

List<ProductDetails> _productsSorted = _products
      ..sort((a, b) => a.price.compareTo(b.price));

Thank you


Solution

  • First of all import collection library;

    import "package:collection/collection.dart";
    

    secondly use groupBy method to group your list item by price;

    Map<dynamic, List> _grouped = groupBy(_products, (p) => p.price);
    

    at last make a new list from the first item of the each value of the _grouped Map;

    List _lastList = [];
    
    _grouped.values.forEach((f) => _lastList.add(f.first));