skip to Main Content

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


  1. 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

    endDate: event.eventStartTime!.add(Duration(minutes:int.parse(event.eventDuration!)))
    

    and

    endDate: event.eventStartTime!.add(Duration(days: event.recurrenceEndDate.difference(event.eventDate).inDays))
    

    So the complete answer will be –

    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(Duration(minutes: int.parse(event.eventDuration!))),  //Removed const here
          allDay: event.allDay,
    
          androidParams: const AndroidParams(
            emailInvites: ["[email protected]"],
          ),
          recurrence:  Recurrence(
            frequency: freq,
            endDate: event.eventStartTime!.add(Duration(days: event.recurrenceEndDate.difference(event.eventDate).inDays)), //Removed Const here
        );
        }
    
    Login or Signup to reply.
  2. You cannot use const for value that is calculated.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search