From 9d033dde7841c8358be0c84613c5df832ffd657b Mon Sep 17 00:00:00 2001 From: shams098 Date: Fri, 15 Nov 2024 15:23:09 +0500 Subject: [PATCH] Handle the null value on time ingetPrayers method --- lib/prayer_services.dart | 65 ++++++++++++++++++++++++---------------- 1 file changed, 40 insertions(+), 25 deletions(-) diff --git a/lib/prayer_services.dart b/lib/prayer_services.dart index 656cf14..b627d8f 100644 --- a/lib/prayer_services.dart +++ b/lib/prayer_services.dart @@ -33,7 +33,8 @@ class PrayerService { List prayersList = []; var scheduledCount = 0; for (var key in prayerKeys) { - var obj = await PrayerNotificationService().getPrayerNotificationFromDB(key); + var obj = + await PrayerNotificationService().getPrayerNotificationFromDB(key); if (check(obj)) { scheduledCount++; } @@ -47,43 +48,57 @@ class PrayerService { while (prayersList.length < (Platform.isIOS ? 70 : 10)) { i++; for (var key in prayerKeys) { - var obj = await PrayerNotificationService().getPrayerNotificationFromDB(key); + var obj = + await PrayerNotificationService().getPrayerNotificationFromDB(key); var index = prayerKeys.indexOf(key); if (check(obj)) { final mosque = await getMosque(obj.mosqueUuid ?? ''); - var time = getPrayerTime(mosque, key, time: DateTime.now().add(Duration(days: i))); - var notificationData = await getPrayerDataByIndex(mosque, index); - var prayerName = _getPrayerName(index); - - String indexStr = index.toString(), dayStr = time!.day.toString(), monthStr = time.month.toString(); - String str = indexStr + dayStr + monthStr; - int alarmId = int.parse(str); - print('Alarm ID: $alarmId'); - - NotificationInfoModel prayer = NotificationInfoModel( - mosqueName: mosque.name, - sound: obj.notificationSound, - prayerName: prayerName, - time: time, - notificationBeforeAthan: notificationData?.notificationBeforeAthan ?? 0, - alarmId: alarmId, - ); - - prayersList.add(prayer); + var time = getPrayerTime(mosque, key, + time: DateTime.now().add(Duration(days: i))); + if (time != null) { + var notificationData = await getPrayerDataByIndex(mosque, index); + var prayerName = _getPrayerName(index); + + String indexStr = index.toString(), + dayStr = time.day.toString(), + monthStr = time.month.toString(); + String str = indexStr + dayStr + monthStr; + int alarmId = int.parse(str); + print('Alarm ID: $alarmId'); + + NotificationInfoModel prayer = NotificationInfoModel( + mosqueName: mosque.name, + sound: obj.notificationSound, + prayerName: prayerName, + time: time, + notificationBeforeAthan: + notificationData?.notificationBeforeAthan ?? 0, + alarmId: alarmId, + ); + prayersList.add(prayer); + } } } } + prayersList.removeWhere((element) { - return element.time!.isBefore(DateTime.now()); + return element.time == null || element.time!.isBefore(DateTime.now()); + }); + + prayersList.sort((a, b) { + if (a.time == null && b.time == null) return 0; // Both null, consider equal + if (a.time == null) return 1; // `a` is null, so `b` should come first + if (b.time == null) return -1; // `b` is null, so `a` should come first + return a.time!.millisecondsSinceEpoch + .compareTo(b.time!.millisecondsSinceEpoch); }); - prayersList.sort( - (a, b) => a.time!.millisecondsSinceEpoch.toString().compareTo(b.time!.millisecondsSinceEpoch.toString()), - ); + prayersList = prayersList.sublist(0, Platform.isIOS ? 63 : 5); return prayersList; } + Future getPrayerDataByIndex(DetailedMosque? mosque, int index) async { final nextPrayerKey = prayerKeys[index]; final notificationData = await PrayerNotificationService().getPrayerNotificationFromDB(nextPrayerKey);