CacheModel.fromMap constructor

CacheModel.fromMap(
  1. dynamic data
)

Factory constructor to create a CacheModel instance from a map. This function attempts to parse the map and create a CacheModel object.

Implementation

factory CacheModel.fromMap(dynamic data) {
  try {
    // Check if the data is a valid Map and contains the required fields
    if (data is Map<String, dynamic> &&
        (data['expirationDateTime'] != null || data['value'] != null)) {
      return CacheModel(
        expirationDateTime: dateTimeFromJson(data[
            'expirationDateTime']), // Converts the expiration time from string to DateTime
        value: data['value'], // Retrieved cached value
      );
    }
  } catch (e) {
    debugPrint(e.toString()); // Log any errors during object creation
  }

  // Fallback to a default CacheModel if creation fails
  return CacheModel(
    expirationDateTime: null, // Defaulting to null if parsing fails
    value: data, // Store raw data as value if no valid structure was found
  );
}