I am trying to add time and days to a couple DateTime values in the add_2_calendar flutter package. This is the documentation for the package: https://pub.dev/packages/add_2_calendar/example
Here is the code I am trying to implement:
class AddEventsToAllCalendars {
addEvent(Events event) {
Add2Calendar.addEvent2Cal(buildEvent(event));
}
Event buildEvent(Events event) {
int eventRecurrenceEndDate = event.eventDate!.difference(event.eventDate!).inDays;
Frequency freq = Frequency.daily;
if (event.frequency == 'daily') {
freq = Frequency.daily;
} else if (event.frequency == 'weekly') {
freq = Frequency.weekly;
} else if (event.frequency == 'monthly') {
freq = Frequency.monthly;
} else {
freq = Frequency.yearly;
}
return Event(
title: event.eventName!,
description: event.eventDescription,
location: event.location,
startDate: event.eventStartTime!,
endDate: event.eventStartTime!.add(const Duration(minutes: int.parse(event.eventDuration!))), <<< ERROR HERE
allDay: event.allDay,
androidParams: const AndroidParams(
emailInvites: ["[email protected]"],
),
recurrence: Recurrence(
frequency: freq,
endDate: event.eventStartTime!.add(const Duration(days: event.recurrenceEndDate.difference(event.eventDate).inDays)), <<< ERROR HERE
);
}
I am getting an error that I don’t know how to fix. Here is the error message:
Evaluation of this constant expression throws an exception.dart(const_eval_throws_exception)
Duration Duration({
int days = 0,
int hours = 0,
int minutes = 0,
int seconds = 0,
int milliseconds = 0,
int microseconds = 0,
})
I think I am providing integers for the .add in both lines of code so what am I doing wrong here?
Thanks for your help
2
Answers
You need to remove constants as by removing the const keyword, you allow the Duration objects to be created at runtime, resolving the constant expression evaluation error.
So endDate attribute should be
and
So the complete answer will be –
You cannot use
const
for value that is calculated.