builttoroam / device_calendar

A cross platform plugin for modifying calendars on the user's device
https://pub.dev/packages/device_calendar
BSD 3-Clause "New" or "Revised" License
259 stars 258 forks source link

How to not create duplicate events with createOrUpdateEvent()? #469

Open Zookey opened 1 year ago

Zookey commented 1 year ago

Hi,

I am adding events to the calendar, but my users are complaining about duplicate events, when they by mistake or intentionally tap on the button to add events to the calendar.

I am using createOrUpdateEvent(), but as far as I can see eventId is a private field and I can not modify it.

Is it possible to create events without duplicates?

SuperKrallan commented 1 year ago

The answer is yes. I have been using device_calendar for some time now, and I have not seen this problem. My guess it is in your code rather than in the code of the device_calendar.

IVLIVS-III commented 1 year ago

Afaik createOrUpdateEvent takes an Event as an argument. If the eventId field is set to null (or the eventId is invalid) a new Event will be created with an Id chosen by the device. This Id will be returned from createOrUpdateEvent. Note: You can theoretically set the eventId field in your Dart-Code to any value of type String?. It will only be honored when it is a valid Id, i.e. the Id of a previously created Event.

In order to update an existing Event the eventId must be the Id of a previously created Event. You can get this Id by first retrieving Events in a relevant timeframe. Note: On iOS this timeframe must not be longer than a couple of years or no Events will be returned.

Steps to update an Event:

  1. retrieve Events in the relevant timeframe
  2. pick the Event you want to update from the returned List<Event>
  3. modify the fields of that Event instance, but do not modify eventId
  4. call createOrUpdateEvent with the modified Event instance