小编fre*_*n29的帖子

Dart:如何减少类列表

我想减少类列表,例如:

class Product {
  final String name;
  final double price;

  Product({this.name, this.price});
}

List<Product> products = [
  Product(name: 'prod1', price: 100.0),
  Product(name: 'prod2', price: 300.0),
];

void main() {
  var sum = products.reduce((curr, next) => curr.price + next.price);
  print(sum);
}
Run Code Online (Sandbox Code Playgroud)

但它会引发错误

错误:无法将“double”类型的值分配给“Product”类型的变量。

dart

4
推荐指数
1
解决办法
1597
查看次数

标签 统计

dart ×1