Home > Blockchain >  Flutter : Generate List of total sales for similar date from another list that has repeated days
Flutter : Generate List of total sales for similar date from another list that has repeated days

Time:03-26

I have a list of daily sales which includes "DateTime" & "Int" for each entry. The issue with this list is that there are some times multiple entries for the same date.

final data = [
      DailySales(2022-03-10 05:25:40.000, 200),
      DailySales(2022-03-10 09:10:50.000, 455),
      DailySales(2022-03-09 04:70:30.000, 255),
      DailySales(2022-03-09 20:10:20.000, 525),
      DailySales(2022-02-08 05:60:00.000, 100),
      DailySales(2022-02-10 00:00:00.000, 520),
      DailySales(2022-01-08 12:10:40.000, 100),
      DailySales(2022-01-08 02:30:10.000, 250),
      DailySales(2022-01-10 09:20:00.000, 520),
    ];

class DailySales {
  final DateTime day;
  final int sales;

  DailySales(this.day, this.sales);
}

Now Based on the above list, I need to dynamically generate a new list that combine the sales in one entry if there is multiple entries of similar date. i.e one entry per date and the total sales for that date.

Can some help on this please

CodePudding user response:

for(int i=0; i<data.length;i  ){

 if(finalList.contains(data[i].day) {
   final tempSales=finalList.firstWhere((element)=>element.day==data[i].day)
   data[i].sales = tempSales.sales
  }
   else{
     finalList.add(data);
   }
 
 }

**I've written the code from my phone.. so there can be a couple of syntax error like no semicolon etc. but i hope you'll get the logic and do upvote if helpful:)

CodePudding user response:

A basic workaround:

I'll be implementing a simple technique on the sample data from your question.

Your data:

final data = [
    DailySales(2022-03-10 00:00:00.000, 200),
    DailySales(2022-03-10 00:00:00.000, 455),
    DailySales(2022-03-09 00:00:00.000, 255),
    DailySales(2022-03-09 00:00:00.000, 525),
    DailySales(2022-02-08 00:00:00.000, 100),
    DailySales(2022-02-10 00:00:00.000, 520),
    DailySales(2022-01-08 00:00:00.000, 100),
    DailySales(2022-01-08 00:00:00.000, 250),
    DailySales(2022-01-10 00:00:00.000, 520),
];

You need to add all the Objects with same date like:

void processData() {
  List<DailySales> processedData = [];

  for (var dailySales in data) {
    int sales = 0;

    final dailyFormat = DateFormat("dd-MM-yyyy").format(dailySales.day);
    
    for (int i = 0; i < data.length; i  ) {

      final dateFromData = DateFormat("dd-MM-yyyy").format(data[i].day);

      if (dateFromData == dailyFormat) {
        sales  = data[i].sales;
      }
    }

    processedData
        .add(DailySales(DateFormat("dd-MM-yyyy").parse(dailyFormat), sales));
    
  }
  
  final Map<DateTime, DailySales> map = {
    for (var dailySales in processedData) dailySales.day : dailySales,  
  };
  
  processedData = map.values.toList();
}

The list named processedData will contain DailySales with one entry for each date.

Update: You need to use intl package for this.

  • Related