dateTimeFromInternet function
Get current DateTime from internet
timezone
Set a timezone. Use Timezones
useHttps
If true use https instead of http to make request to the worldtimeapi.org
if null will return a timezone from public client ip
Implementation
Future<DateTime> dateTimeFromInternet({
String? timezone,
bool useHttps = false,
}) async {
var response = await http.get(
Uri.parse(
"http${useHttps ? 's' : ''}://worldtimeapi.org/api/${timezone != null ? 'timezone/$timezone' : 'ip'}"),
headers: {'accept': 'application/json'});
var json = jsonDecode(response.body);
if (response.statusCode != 200) {
throw Exception([response.body, response.request?.url]);
}
return DateTime.parse(json['datetime'] as String).toLocal();
}