扑火基地列表<model>组用于计数值

时间:2020-09-22 13:30:11

标签: flutter dart

所以我有一个模型来显示我从firebase取得的订单

refund policy

和另一个显示报告顺序的模型

<Button Margin="10,20,10,10" Height="37" Command="{Binding AddCmd}">View all customers</Button>
....
<TabControl x:Name="myTabs" ItemsSource="{Binding WorkSpaces}" Height="323">
                            <TabControl.Resources>
                                <DataTemplate DataType="{x:Type viewmodel:AllCustomersViewModel}">
                                    <views:AllCustomersView/>
                                </DataTemplate>
                            </TabControl.Resources>
                            <TabControl.ItemTemplate>
                                ....
                            </TabControl.ItemTemplate>
                        </TabControl>

我从firebase得到的结果是

public MainWindow()
        {
            InitializeComponent();
            this.DataContext = new DemoWPFApp.ViewModel.MainWindowViewModel();
            myTabs.DataContext = this.DataContext; //that is where the problem is
        }

我怎么能把它变成这样?

class Order {
  String buyerId;
  String buyerName;
  String price;

  Order({this.buyerId, this.buyerName, this.price});
}

更新:

我要询问的是在客户端/ Flutter / Dart上转换此列表,而不是在Firebase云功能中转换

2 个答案:

答案 0 :(得分:0)

您可以做的一种方法就是这样。

  List<Order> orders = [
    Order(buyerId: "0001", buyerName: "Mr.X", price: "20"),
    Order(buyerId: "0002", buyerName: "Ms.Y", price: "20"),
    Order(buyerId: "0002", buyerName: "Ms.Y", price: "20"),
    Order(buyerId: "0002", buyerName: "Ms.Y", price: "20"),
  ];
  
  List<ReportOrder> list = [];
  
  for(int j = 0; j < orders.length; j++){
    Order order = orders[j];
    for(int i = 0; i <= list.length; i++){
      if(i == list.length){
        list.add(ReportOrder(buyerId: order.buyerId, buyerName: order.buyerName , priceSum: order.price, count: "1"));
        break;
      }
      if(order.buyerId == list[i].buyerId){
        list[i].priceSum = "${int.parse(list[i].priceSum) + int.parse(order.price)}";
        list[i].count = "${int.parse(list[i].count)+1}";
        break;
      }
    }
  }

答案 1 :(得分:0)

首先:请修复文件类型:pricepriceSum应该是double,而count应该是int

从Dart 2.7开始,您可以使用扩展程序

extension ReportOrderList on List<ReportOrder> {
  void putOrSum(Order order) {
    final index = indexWhere((report) => report.buyerId == order.buyerId);
    if (-1 == index) {
      add(ReportOrder(
        buyerId: order.buyerId,
        buyerName: order.buyerName,
        priceSum: order.price,
        count: 1,
      ));
    } else {
      this[index] = ReportOrder(
        buyerId: order.buyerId,
        buyerName: order.buyerName,
        priceSum: this[index].priceSum + order.price,
        count: ++this[index].count,
      );
    }
  }
}

然后您就不必关心OrderReport列表中的重复项了:

  var list = <Order>[
    Order(buyerId: '0001', buyerName: 'X', price: 20),
    Order(buyerId: '0002', buyerName: 'Y', price: 20),
    Order(buyerId: '0002', buyerName: 'Y', price: 20),
    Order(buyerId: '0002', buyerName: 'Y', price: 20),
  ];

  var newList = <ReportOrder>[];

  for (var obj in list) {
    newList.putOrSum(obj);
  }