getNetworkTime method
Fetches the current network time using NTP. Falls back to locally stored NTP time if fetching fails.
Implementation
Future<DateTime?> getNetworkTime() async {
try {
DateTime ntpTime = await NTP.now().timeout(
const Duration(seconds: 20),
onTimeout: () {
safeLog('NTP request timed out after 20 seconds');
throw TimeoutException(
'NTP request timed out', const Duration(seconds: 5));
},
);
safeLog('Network time: $ntpTime');
localDataSource.storeNetworkTime(networkTime: ntpTime);
return ntpTime;
} catch (e) {
DateTime? storedNTPTime = await _getNetworkTimeStoredOffline();
safeLog('Stored Network time: $storedNTPTime', error: e);
return storedNTPTime; // fallback to device time
}
}