getFormattedDate method

String? getFormattedDate(
  1. String? parametersJson,
  2. String parameterKey
)

Implementation

String? getFormattedDate(String? parametersJson, String parameterKey) {
  if (parametersJson == null) return null;

  try {
    // Decode the parameters JSON string into a Map
    final Map<String, dynamic> parameters = json.decode(parametersJson);

    // Get the value for the specified parameter key
    String? dateString = parameters[parameterKey];

    // If a valid date string is found, format it
    if (dateString != null) {
      // Parse the date string into a DateTime object
      DateTime parsedDate = DateTime.parse(dateString);

      // Format the date as 'dd-MM-yyyy'
      return DateFormat('EEE, dd-MM-yyyy').format(parsedDate);
    }

    return null;
  } catch (e) {
    // In case of any error (invalid JSON or invalid date), return the default value
    return 'Thu, 10-10-2024';
  }
}