getServerTimestamp method
Implementation
Future<String> getServerTimestamp() async {
try {
final response = await http
.get(Uri.parse('https://worldtimeapi.org/api/timezone/Etc/UTC'));
if (response.statusCode == 200) {
final data = jsonDecode(response.body);
final datetime = DateTime.parse(data['utc_datetime']);
return DateFormat('yyyyMMdd').format(datetime);
} else {
throw Exception('Failed to fetch server date');
}
} catch (e) {
print("Failed to fetch server date: '${e.toString()}'.");
return DateFormat('yyyyMMdd').format(DateTime.now());
}
}