You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Flutter如何基于两个列表数据填充ProductWithPrice列表?

Dart模型列表映射与字段填充实现方案

问题背景

我有三个简化后的Dart模型类:

class Product extends Object {
  int? type;
  Product({this.type});
  Product.fromJson(Map<String, dynamic> json) {
    type = json["type"];
  }
}

class ProductWithPrice extends Object {
  int? type;
  double? price;
  ProductWithPrice({this.type, this.price});
  ProductWithPrice.fromJson(Map<String, dynamic> json) {
    type = json["type"];
    price = json["price"];
  }
}

class Prices extends Object {
  int? type;
  double? price;
  Prices({this.type, this.price});
  Prices.fromJson(Map<String, dynamic> json) {
    type = json["type"];
    price = json["price"];
  }
}

现有列表数据(假设orange、apple是预定义的int常量):

List<Product> _productList = [Product(type: orange), Product(type: apple)];
List<ProductWithPrice> _productWithPriceList = []; // 空列表
List<Prices> _pricesList = [Prices(type: orange, price: 100), Prices(type: apple, price: 200)];

需要完成两个操作:

  1. 将_productList的内容映射复制到_productWithPriceList中,不确定多出来的price字段应该设为null还是省略?
  2. 填充完成后,根据type字段匹配,把_pricesList中的price值复制到_productWithPriceList中。

最终期望结果:

List<ProductWithPrice> _productWithPriceList = [
  ProductWithPrice(type: orange, price: 100),
  ProductWithPrice(type: apple, price: 200)
];

实现步骤

步骤1:转换Product列表到ProductWithPrice列表

因为ProductWithPrice的price是可空类型(double?),初始化时直接设为null即可——Dart中可空字段默认值就是null,显式声明更清晰,也不影响后续赋值。

用map方法快速完成列表转换:

_productWithPriceList = _productList.map((product) {
  return ProductWithPrice(type: product.type);
}).toList();

步骤2:匹配type填充price字段

为了提升匹配效率,先将_pricesList转为以type为键、price为值的Map,这样查找操作的时间复杂度为O(1),远优于嵌套遍历的O(n²):

// 转换为Map,快速匹配type对应的price
final priceMap = {for (var p in _pricesList) p.type: p.price};

// 遍历列表填充price
for (var item in _productWithPriceList) {
  item.price = priceMap[item.type];
}

完整可运行代码

void main() {
  // 定义示例常量
  const orange = 1;
  const apple = 2;

  List<Product> _productList = [Product(type: orange), Product(type: apple)];
  List<ProductWithPrice> _productWithPriceList = [];
  List<Prices> _pricesList = [Prices(type: orange, price: 100), Prices(type: apple, price: 200)];

  // 步骤1:转换列表
  _productWithPriceList = _productList.map((product) => ProductWithPrice(type: product.type)).toList();

  // 步骤2:填充price字段
  final priceMap = {for (var p in _pricesList) p.type: p.price};
  for (var item in _productWithPriceList) {
    item.price = priceMap[item.type];
  }

  // 打印验证结果
  _productWithPriceList.forEach((item) {
    print("type: ${item.type}, price: ${item.price}");
  });
}

// 修正后的模型类(添加this参数,符合Dart规范)
class Product extends Object {
  int? type;
  Product({this.type});
  Product.fromJson(Map<String, dynamic> json) {
    type = json["type"];
  }
}

class ProductWithPrice extends Object {
  int? type;
  double? price;
  ProductWithPrice({this.type, this.price});
  ProductWithPrice.fromJson(Map<String, dynamic> json) {
    type = json["type"];
    price = json["price"];
  }
}

class Prices extends Object {
  int? type;
  double? price;
  Prices({this.type, this.price});
  Prices.fromJson(Map<String, dynamic> json) {
    type = json["type"];
    price = json["price"];
  }
}

补充说明

  • 关于price字段:可空类型省略赋值时默认就是null,和显式声明price: null效果一致,但显式写法可读性更强。
  • Map匹配的优势:当列表数据量较大时,这种方式能显著提升性能,避免不必要的嵌套遍历。

内容的提问来源于stack exchange,提问作者BradG

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.01 10:42:54