Map<DateTime, List<CleanCalendarEvent>> events = {};
example:
DateTime(DateTime.now().year, DateTime.now().month, DateTime.now().day): [
CleanCalendarEvent('Event A',
startTime: DateTime(DateTime.now().year, DateTime.now().month,
DateTime.now().day, 10, 0),
endTime: DateTime(DateTime.now().year, DateTime.now().month,
DateTime.now().day, 12, 0),
description: 'A special event',
color: Colors.orangeAccent),
],
DateTime(DateTime.now().year, DateTime.now().month, DateTime.now().day 2):
[
CleanCalendarEvent('Event B',
startTime: DateTime(DateTime.now().year, DateTime.now().month,
DateTime.now().day 2, 10, 0),
endTime: DateTime(DateTime.now().year, DateTime.now().month,
DateTime.now().day 2, 12, 0),
color: Colors.orange),
CleanCalendarEvent('Event C',
startTime: DateTime(DateTime.now().year, DateTime.now().month,
DateTime.now().day 2, 14, 30),
endTime: DateTime(DateTime.now().year, DateTime.now().month,
DateTime.now().day 2, 17, 0),
color: Colors.pink),
],
}
In this Map, I want to check if the Date time object I'm getting already exists or not. If it does then add CleanCalendarEvent to the list of that particular DateTime object in the map. and if it doesn't then add a new key-value pair of DateTime and List.
Here's my approach but it isn't working
if (events.containsKey(DateTime(date2.year, date2.month, date2.day))) {
events[DateTime(date2.year, date2.month, date2.day)]!.add(
CleanCalendarEvent('BookingText',
startTime: DateTime(now.year, now.month, now.day 1, 10, 0),
endTime: DateTime(now.year, now.month, now.day 1, 12, 0),
description: 'A special event',
color: Colors.orangeAccent));
} else {
events[DateTime(date2.year, date2.month, date2.day)] = [
CleanCalendarEvent('BookingText',
startTime: DateTime(now.year, now.month, now.day 1, 10, 0),
endTime: DateTime(now.year, now.month, now.day 1, 12, 0),
description: 'A special event',
color: Colors.orangeAccent)
];
}
CodePudding user response:
This works in dartpad - I took your code and just simplified it a bit. Not sure where you have the issue.
Map<DateTime, List<String>> events = {};
void addMapValue(DateTime d, String value) {
if (events.containsKey(d)) {
events[d]!.add(value);
} else {
events[d] = [value];
}
}
void main() {
events[DateTime(2022,6,20)]=['a'];
events[DateTime(2022,6,21)]=['b'];
events[DateTime(2022,6,22)]=['c'];
addMapValue(DateTime(2022,6,20), 'x');
addMapValue(DateTime(2022,6,30), 'y');
print(events);
}
Output is:
{
2022-06-20 00:00:00.000: [a, x],
2022-06-21 00:00:00.000: [b],
2022-06-22 00:00:00.000: [c],
2022-06-30 00:00:00.000: [y]
}
CodePudding user response:
The answer, instead of passing a new obj of DateTime now I'm passing date
if (events.containsKey(date2)) {
events[date2]!.add(CleanCalendarEvent('BookingText',
startTime: DateTime(now.year, now.month, now.day 1, 10, 0),
endTime: DateTime(now.year, now.month, now.day 1, 12, 0),
description: 'A special event',
color: Colors.orangeAccent));
} else {
events[date2] = [
CleanCalendarEvent('BookingText',
startTime: DateTime(now.year, now.month, now.day 1, 10, 0),
endTime: DateTime(now.year, now.month, now.day 1, 12, 0),
description: 'A special event',
color: Colors.orangeAccent)
];
}