I nave list of objects , e.g.
List<Product> products = [product1, product2, product1, product2, product1, product1]
How I can to get list of unique objects with their number from the List?
import 'package:built_value/serializer.dart';
import 'package:built_value/built_value.dart';
part 'product.g.dart';
abstract class Product implements Built<Product, ProductBuilder>{
int get id;
String get title;
String get image;
double get price;
int get volume;
static Serializer<Product> get serializer => _$productSerializer;
Product._();
factory Product([updates(ProductBuilder b)]) = _$Product;
}
I would like to get other List with objects:
class OrderPosition {
int id;
String title;
int count; // number of unique elements from list 'products'
}
For example:
List<OrderPosition> = [
OrderPosition(1, title1, 4),
OrderPosition(2, title2, 2)
]
class Product {
Product(this.id); // for operator == to work properly
final int id; // make at least the id immutable
String title;
String image;
double price;
int volume;
bool operator ==(Object other) => identical(this, other) || (other as Product).id == id;
int get hashCode => id.hashCode;
}
var uniqueProducts = products.toSet().toList();
var result = <OrderPosition>[];
for(var i = 0; i < uniqueProducts.length; i++) {
result.add(
OrderPosition(i,
uniqueProducts[i].title,
products.where((e) => e == uniqueProducts[i]).length)));
}
Another alternative, using Set.putIfAbsent, is to collect the information in one iteration:
var uniqueProductMap = Map<Product, OrderPosition>.identity();
for (var product in products) {
var position = uniqueProductMap.putIfAbsent(product,
() => OrderPosition(product.id, product.title, 0);
position.count++;
}
var uniqueProducts = uniqueProductMap.values.toList();
This uses identical for deciding which elements are unique. If you want to use equality instead, replace the map with just <Product, OrderPosition>{}. If you want to use another equivalence not supported by the object itself, you can use:
var uniqueProductMap = LinkedHashMap<Product, OrderPosition>(
equals: (p1, p2) => p1.id == p2.id,
hashCode: (p) => p.id.hashCode);
(I fully support the recommendation to make id final if you use it for equality comparisons).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With