Skip to main content
GuidesAPI ReferenceExamplesFAQ

Weather Service

Estimated reading time: 3 minutes

The WeatherService class contains methods for getting the current, hourly and daily forecasts.

Get Current Weather Forecast

To retrieve the current weather forecast, use the static getCurrent method of the WeatherService class. Provide the coordinates for the desired location, and the forecast will be returned through the onCompleteCallback. The following example demonstrates how to fetch the current forecast for Paris coordinates.

    final locationCoordinates = Coordinates(
latitude: 48.864716,
longitude: 2.349014,
);
final weatherCurrentCompleter = Completer<List<LocationForecast>>();

WeatherService.getCurrent(
coords: [locationCoordinates],
onCompleteCallback: (err, result) async {
weatherCurrentCompleter.complete(result);
},
);

final currentForecast = await weatherCurrentCompleter.future;
showSnackbar("Forecast lenght list: ${currentForecast.length}");
note

The result will contain as many LocationForecast objects as the list size of given coordinates to coords parameter.

Get Hourly Weather Forecast

To retrieve the hourly weather forecast, use the static getHourlyForecast method of the WeatherService class. Provide the coordinates for the desired location, and the forecast will be returned through the onCompleteCallback. The following example demonstrates how to fetch the hourly forecast for Paris coordinates.

  final locationCoordinates = Coordinates(
latitude: 48.864716,
longitude: 2.349014,
);

final weatherHourlyCompleter = Completer<List<LocationForecast>>();

WeatherService.getHourlyForecast(
hours: 24,
coords: [locationCoordinates],
onCompleteCallback: (err, result) async {
weatherHourlyCompleter.complete(result);
},
);

final hourlyForecast = await weatherHourlyCompleter.future;
showSnackbar("Forecast lenght list: ${hourlyForecast.length}");

You'll need to provide the number of hours for which the forecast is requested.

warning

The number of requested hours must not exceed 240. Exceeding this limit will result in an empty response and an error of type GemError.outOfRange.

Get the Daily Forecast

To retrieve the daily weather forecast, use the static getDailyForecast method of the WeatherService class. Provide the coordinates for the desired location, and the forecast will be returned through the onCompleteCallback. The following example demonstrates how to fetch the daily forecast for Paris coordinates.

  final locationCoordinates = Coordinates(
latitude: 48.864716,
longitude: 2.349014,
);

final weatherDailyCompleter = Completer<List<LocationForecast>>();

WeatherService.getDailyForecast(
days: 10,
coords: [locationCoordinates],
onCompleteCallback: (err, result) async {
weatherDailyCompleter.complete(result);
},
);

final dailyForecast = await weatherDailyCompleter.future;
showSnackbar("Forecast lenght list: ${dailyForecast.length}");

You'll need to provide the number of days for which the forecast is requested.

warning

The number of requested days must not exceed 10. Exceeding this limit will result in an empty response and an error of type GemError.outOfRange.

Get the Weather Forecast

To retrieve the weather forecast at whatever time and coordinates, use the static getForecast method of WeatherService class. You'll need to provide a list of WeatherDurationCoordinates, and the onCompleteCallback will retrieve as many locationForecasts as the size of WeatherDurationCoordinates list.

final weatherCompleter = Completer<List<LocationForecast>>();

WeatherService.getForecast(
coords: [
WeatherDurationCoordinates(
coordinates: Coordinates(
latitude: 48.864716,
longitude: 2.349014,
),
duration: Duration(days: 2))
],
onCompleteCallback: (err, result) async {
weatherCompleter.complete(result);
});

final forecast = await weatherCompleter.future;

showSnackbar("Forecast lenght list: ${forecast.length}");
note

The duration parameter in WeatherDurationCoordinates specifies the time offset into the future for which the forecast is requested.