getEvents function

This method retrieves a list of events from the remote database It retrieves all keys that match the regex pattern 'createevent-' If no matching keys are found, it returns an empty list The method then fetches the event data associated with each key and converts it into an EventNotificationModel object The event objects are added to the 'allEvents' list Finally, the method returns the 'allEvents' list containing the retrieved events If any error occurs during the process, it returns null

Implementation

Future<List<EventNotificationModel>?> getEvents() async {
  var allEvents = <EventNotificationModel>[];
  var regexList = await EventService().atClientManager.atClient.getKeys(
        regex: 'createevent-',
      );

  if (regexList.isEmpty) {
    return [];
  }

  try {
    for (var i = 0; i < regexList.length; i++) {
      var atkey = EventService().getAtKey(regexList[i]);
      var atValue = await EventService().atClientManager.atClient.get(atkey);
      if (atValue.value != null) {
        var event = EventNotificationModel.fromJson(jsonDecode(atValue.value));
        allEvents.add(event);
      }
    }

    WidgetsBinding.instance.addPostFrameCallback((timeStamp) {});
    return allEvents;
  } catch (e) {
    print(e);
    return null;
  }
}