我有一个
class shop {
int id;
String name;
List<Product> products;
}
class Product {
int id;
String productName;
}
每个商店都有自己的产品,如何将所有商店的产品合并到一个列表中 如何创建所有产品的列表
最佳答案
您可以使用+或展开运算符合并列表
使用加法运算符:
List<shop> shops = [shop1, shop2];
List<Product> products = shop1.products + shop2.products;
使用扩展运算符:
List<Product> products = [...shop1.products, ...shop2.products];
编辑
你需要这样做:
List<shop> shops = [shop1, shop2,...];
List<Product> mergedProducts = []
for(int i = 0; i < shops.length; i++){
mergedProducts = mergedProducts + shops[i].products;
}
关于list - 如何将多个列表合并为一个列表? Dart ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61069808/