【问题标题】:Flutter Calculating the total price of the items in thier categoryFlutter 计算所属类别中商品的总价
【发布时间】:2020-12-17 14:16:57
【问题描述】:

待办事项应用

class ToDoItem{
  final String id;
  final String title;
  final Color color;
  final int price;
  final IconData icon;
  final String todoListId;

  const ToDoItem({
    @required this.color,
    @required this.id,
    @required this.todoListId,
    @required this.price,
    @required this.title,
    @required this.icon,
  });}

我正在努力将待办事项列表中的项目总价格加起来等于 todoListId 例如:

列表屏幕

列表项目屏幕

所以我想计算总商品价格

这是我尝试过的:

  List<Map<String, Object>> get totalItemsPrice{
    return List.generate(1, (index) {
      var totalPriceSum = 0.0;
      for (var i = 0; i < toDo.length; i++) {
        totalPriceSum = toDo[index].amount + totalPriceSum ;
      }
      return {
        'price': totalSum,
      };
    }).toList();
  }

  double get totalPriceSum {
    return totalItemsPrice.fold(0.0, (sum, item) {
      return sum + item['price'];
    });
  }

待办事项如下所示:

List<ToDO> toDo= [];

但是当我希望它只计算其类别中物品的总价时,它正在计算所有待办事项的总价,但它不起作用

【问题讨论】:

    标签: list android-studio flutter dart


    【解决方案1】:

    如果要根据项目的todoListId 进行过滤,则需要将其提供给方法。从那里,在fold 之前使用where 以仅对具有匹配ID 的项目求和:

    double totalPriceSum(int todoListId) {
      return ItemsPrice.where((item) => item.todoListId == todoListId)
                       .fold(0.0, (sum, item) => sum + item['price']); 
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-05-26
      • 2019-12-09
      • 2023-01-12
      • 1970-01-01
      • 2021-04-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多