removeDuplicates method

List<EventRequest> removeDuplicates(
  1. List<EventRequest> eventRequests
)

Implementation

List<EventRequest> removeDuplicates(List<EventRequest> eventRequests) {
  final Map<String, EventRequest> filteredRequests = {};
  for (var event in eventRequests) {
    final String? piClientTs = event.data[PerceptSDK.clientTs.asString];
    final String? piClientUniqueEventId =
        event.data[PerceptSDK.clientUniqueEventId.asString];

    if (piClientTs != null && piClientUniqueEventId != null) {
      final String key = '$piClientTs-$piClientUniqueEventId';

      if (!filteredRequests.containsKey(key)) {
        filteredRequests[key] = event;
      } else {
        EventRequest? uniqueEvent = filteredRequests[key];
        if (uniqueEvent != null) {
          String duplicateCount =
              uniqueEvent.data[PerceptSDK.duplicateCount.asString] ?? '0';
          int duplicateCountInt = int.tryParse(duplicateCount) ?? 0;
          uniqueEvent.data[PerceptSDK.duplicateCount.asString] =
              (duplicateCountInt + 1).toString();
          filteredRequests[key] = uniqueEvent;
        }
      }
    }
  }
  return filteredRequests.values.toList();
}