Home > Software design >  Flutter : Generate List of total sales for each month from another list of DateTime
Flutter : Generate List of total sales for each month from another list of DateTime

Time:03-12

I have a list of daily sales which includes "DateTime" & "Int" for each entry.

final data = [
      DailySales(2022-03-10 00:00:00.000, 200),
      DailySales(2022-03-09 00:00:00.000, 255),
      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-10 00:00: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 shall includes one entry per Month of month name and the total sales for that month.

as shown in the model below:

class MonthlySales {
  final String month;
  final int sales;

  MonthlySales(this.month, this.sales);
}

Can some help on this please

CodePudding user response:

Here is what you need to do:-

void main() {
  final data = [
    DailySales(day: DateTime(2022, 03, 09), sales: 200),
    DailySales(day: DateTime(2022, 03, 08), sales: 255),
    DailySales(day: DateTime(2022, 03, 07), sales: 100),
    DailySales(day: DateTime(2022, 03, 04), sales: 520),
    DailySales(day: DateTime(2022, 03, 02), sales: 100),
    DailySales(day: DateTime(2022, 03, 03), sales: 520),
  ];

  List<MonthlySales> monthlySales = data
      .map((dailySale) => MonthlySales(
          month: dailySale.day.month.toString(), sales: dailySale.sales))
      .toList();

  print(monthlySales.runtimeType);
  print(monthlySales.toString());
}

class DailySales {
  final DateTime day;
  final int sales;

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

class MonthlySales {
  final String month;
  final int sales;

  MonthlySales({required this.month, required this.sales});
}

And the output is below:-

List<MonthlySales>
[Instance of 'MonthlySales', Instance of 'MonthlySales', Instance of 'MonthlySales', Instance of 'MonthlySales', Instance of 'MonthlySales', Instance of 'MonthlySales']

Dart collections provide many such rich methods like, map methods above to make the business logics simple.

Once visit it for more help.

CodePudding user response:

you can do it like this:

main() {
  final data = [
    DailySales(DateTime.parse("2022-03-10 00:00:00.000"), 200),
    DailySales(DateTime.parse("2022-03-09 00:00:00.000"), 255),
    DailySales(DateTime.parse("2022-02-08 00:00:00.000"), 100),
    DailySales(DateTime.parse("2022-02-10 00:00:00.000"), 520),
    DailySales(DateTime.parse("2022-01-08 00:00:00.000"), 100),
    DailySales(DateTime.parse("2022-01-10 00:00:00.000"), 520),
  ];

  Map tempMap = Map();

  for (DailySales item in data) {
    if (tempMap[item.day.month] == null) {
      tempMap[item.day.month] = item.sales;
    } else {
      tempMap[item.day.month] = tempMap[item.day.month]   item.sales;
    }
  }
  List monthlySales = [];
  for (int i = 1; i <= 12; i  ) {
    if (tempMap[i] != null)
      monthlySales.add(MonthlySales(i.toString(), tempMap[i]));
  }
  print(monthlySales);
}

class DailySales {
  final DateTime day;
  final int sales;

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

class MonthlySales {
  final String month;
  final int sales;

  MonthlySales(this.month, this.sales);
  @override
  String toString() {
    // TODO: implement toString
    return "month "   month   " sales "   sales.toString();
  }
}

the output would look like:

[month 1 sales 620, month 2 sales 620, month 3 sales 455]

CodePudding user response:

You can use this package to groupdata collection

collection: ^1.15.0

 List<MonthlySales> OrderSalebymonth() {
  List<String> months = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'];
  final datas = [DailySales(DateTime.parse("2022-03-10 00:00:00.000"), 200), DailySales(DateTime.parse("2022-03-09 00:00:00.000"), 255), DailySales(DateTime.parse("2022-02-08 00:00:00.000"), 100), DailySales(DateTime.parse("2022-02-10 00:00:00.000"), 520), DailySales(DateTime.parse("2022-01-08 00:00:00.000"), 100), DailySales(DateTime.parse("2022-01-10 00:00:00.000"), 520),];
  List<MonthlySales> monthlist = [];
  var groupBy2 = groupBy(datas, (DailySales sale) => sale.day.month);
  groupBy2.forEach((key, value) {print("=================Month:$key ${months[key - 1]}=============");value.forEach((element) {monthlist.add(MonthlySales(months[key - 1], element.sales));print(element.sales.toString());});});
  return monthlist;
}
class DailySales {
  final DateTime day;
  final int sales;

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

class MonthlySales {
  final String month;
  final int sales;

  MonthlySales(this.month, this.sales);
}

Expected output

=================Month:3 March=============
200
255
=================Month:2 February=============
100
520
=================Month:1 January=============
100
520
  • Related