mapToOptions<T> method

Map<String, Map> mapToOptions<T>(
  1. String key,
  2. String title
)

Maps the current iterable to a Map where each key represents a unique date and the corresponding value is a List of elements assigned on that date.

Returns a Map where each key is a DateTime representing a date, and the value is a List of elements assigned on that date.

Example:

class MyObject {
  DateTime assignedAtDateOnly;

  MyObject(this.assignedAtDateOnly);
}

List<MyObject> objects = [
  MyObject(DateTime(2022, 1, 1)),
  MyObject(DateTime(2022, 1, 1)),
  MyObject(DateTime(2022, 1, 2)),
];

Map<DateTime, List<MyObject>> mapped = objects.mapToAssigned();
print(mapped);
// Output: {2022-01-01: [Instance of 'MyObject', Instance of 'MyObject'], 2022-01-02: [Instance of 'MyObject']}

Implementation

Map<String, Map> mapToOptions<T>(String key, String title) {
  Map<String, Map> mapped = {};
  Iterable<dynamic> objects = this;

  for (var object in objects) {
    mapped[object[key]] = {'title': object[title]};
  }

  return mapped;
}