Home > front end >  Flutter/Dart how map an OBJECT
Flutter/Dart how map an OBJECT

Time:09-26

I have the following object that will return the following structure Map<int, List<Map<String, dynamic>>>

I need now to convert that object to be Map<int, List<StatisticsData>>, so how do I achieve that?!

I tried

Map<int, List<StatisticsData>> d = groupByType.map(
  (key, value) => {
    key: value.map(
      (e) => StatisticsData(e['date'], e['total'])
    ).toList()
  }
);

but it always pops an error! that:

The return type 'Map<int, List<StatisticsData>>' isn't a 'MapEntry<int, List<StatisticsData>>', as required by the closure's context

My code:

final List<Map<String, dynamic>> data = rows.map(
  (row) {
    final String date = row.read<String>('date');
    final double total = row.read<double>('total');
    final int type = row.<int>read('type');

    return {
      'date': date,
      'total': total,
      'type': type
    };

  }
).toList();

Map<int, List<Map<String, dynamic>>> groupByType = groupBy(
  data, 
  ( Map<String, dynamic> obj ) => obj['type']
);

Map<int, List<StatisticsData>> statisticsData;

The StatisticsData class:

class StatisticsData {
  final String name;
  final dynamic value;

  StatisticsData(this.name, this.value);

  
}

CodePudding user response:

As the error message says, you must return a MapEntry:

  Map<int, List<StatisticsData>> d = groupByType.map(
    (key, value) => MapEntry(
      key,
      value.map((e) => StatisticsData(e['date'], e['total'])).toList(),
    ),
  );
  • Related