list - 如何将多个列表合并为一个列表? Dart

标签 list flutter dart merge

我有一个

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/

相关文章:

testing - 分发 Flutter 应用程序 Android 和 iOS 的最佳方式。 Flutter Beta 分发的最佳实践

flutter - 在 Flutter 中使用 PopUpMenuItem onTap/onPessed 的正确方法是什么?

android - 我在 Flutter 中找不到一些图标,比如 Signal 天线

python - 使用 Python 列表推导计算列表中的正整数元素

excel - 通过 VBA 在 Sharepoint 列表中添加和更新单个项目

java - 如何将对象添加到 Java 列表中?

c++ - 迭代器如何在 C++ 中的列表上工作?

node.js - 如何将 flutter 应用程序与 node.js 集成

flutter - 对齐底部导航中的小部件覆盖正文

android - 如何防止在我的 flutter 应用程序的特定部分截屏?