getResults static method

Future<SunriseSunsetResponse?> getResults({
  1. required DateTime date,
  2. required double latitude,
  3. required double longitude,
})

Get results from the Sunrise Sunset API for the given date, latitude, and longitude.

Implementation

static Future<SunriseSunsetResponse?> getResults({
  required DateTime date,
  required double latitude,
  required double longitude,
}) async {
  try {
    Uri uri = Uri(
      scheme: 'https',
      host: 'api.sunrise-sunset.org',
      path: '/json',
      queryParameters: {
        'date': "${date.year}-${date.month}-${date.day}",
        'lat': latitude.toString(),
        'lng': longitude.toString(),
        'formatted': '0'
      },
    );
    // print('Calling ${uri.toString()}');

    http.Response response;
    try {
      response = await http.get(uri);
    } on Exception catch (e) {
      print('Network Error: $e');
      rethrow;
    }

    final jsonResponse = json.decode(response.body);
    return SunriseSunsetResponse.fromJSON(jsonResponse);
  } catch (err) {
    print('Problem with URI: $err');
    return null; // when there is an error, return null
  }
}