getUnixTimeServer static method

Future<int> getUnixTimeServer()

Implementation

static Future<int> getUnixTimeServer() 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);
      return data['unixtime'];
    } else {
      return DateTime.now().millisecondsSinceEpoch ~/ 1000;
    }
  } catch (e) {
    return DateTime.now().millisecondsSinceEpoch ~/ 1000;
  }
}