List<Group> mergedGroups(List<Group> seperateGroups) {
List<Group> mergedGroups = [];
for (var group in seperateGroups) {
Group isExistingGroup = mergedGroups.firstWhere(
(g) => g.dateFormatted == group.dateFormatted,
orElse: () => null,
);
if (isExistingGroup != null) {
isExistingGroup.events.addAll(group.events);
} else {
mergedGroups.add(Group(
dateFormatted: group.dateFormatted,
events: group.events.toList(),
));
}
}
return mergedGroups;
}
This might work. You want to combine a list of group object. Here you pass groups to the function as parameter and it checks whether a group exists with formattedDate or not. If exists, add events to that group else it creates a group. (If your formattedDate has hour:min:s:ms values it does not work. Be sure formattedDate has only year/month/day values.