import 'package:collection/collection.dart';
class Location {
int locationId;
String row;
String level;
String bay;
String position;
Location(this.locationId, this.row, this.level, this.bay, this.position);
}
class Stock {
Location location;
String name;
Stock(this.location, this.name);
}
void main() {
List<Stock> stocks = [
Stock(Location(1, 'A', '1', '1', '1'), 'Stock 1'),
Stock(Location(2, 'B', '2', '2', '2'), 'Stock 2'),
Stock(Location(1, 'A', '1', '1', '1'), 'Stock 3'),
Stock(Location(3, 'C', '3', '3', '3'), 'Stock 4'),
Stock(Location(2, 'B', '2', '2', '2'), 'Stock 5'),
];
Map<int, List<Stock>> groupedStocks = groupBy(stocks, (Stock stock) => stock.location.locationId);
groupedStocks.forEach((locationId, stocks) {
print('Location $locationId:');
stocks.forEach((stock) {
print(' - ${stock.name}');
});
});
}