getPrayerTimeByDate method

Future<PrayerTime?> getPrayerTimeByDate(
  1. String zone,
  2. DateTime date
)

Fetches prayer times for a specific date.

This is a convenience method that fetches prayer times for the month containing the specified date and then filters out the specific day's prayer time.

Parameters:

  • zone: The zone code (e.g., "SGR01"). A list of valid zones can be obtained from getZones or getStates.
  • date: The specific date for which to fetch prayer times (e.g., DateTime(2025, 4, 15)).

Returns a Future<PrayerTime> containing the prayer times for the specified date upon success. Returns null if no prayer time is found for the specified date. Throws WaktuSolatApiException on failure (e.g., invalid zone, network error).

Implementation

Future<PrayerTime?> getPrayerTimeByDate(String zone, DateTime date) async {
  // Extract year and month from the date
  final year = date.year;
  final month = date.month;
  final day = date.day;

  // Get prayer times for the entire month
  final solatV2 = await getPrayerTimesByZone(zone, year: year, month: month);

  // Find the prayer time for the specific date
  // First try to match by the Gregorian date string (YYYY-MM-DD)
  final dateString =
      '${date.year}-${date.month.toString().padLeft(2, '0')}-${date.day.toString().padLeft(2, '0')}';

  // Look for a prayer time with a matching date string
  PrayerTime? prayerTime = solatV2.prayerTime.firstWhere(
    (pt) => pt.date == dateString,
    orElse: () =>
        PrayerTime(hijri: '', day: -1), // Dummy value to indicate not found
  );

  // If not found by date string, try to find by day of month
  if (prayerTime.day == -1) {
    prayerTime = solatV2.prayerTime.firstWhere(
      (pt) => pt.day == day,
      orElse: () =>
          PrayerTime(hijri: '', day: -1), // Dummy value to indicate not found
    );
  }

  // Return null if no matching prayer time was found
  return prayerTime.day == -1 ? null : prayerTime;
}