Weather Service
The WeatherService class provides methods for retrieving current, hourly, and daily weather forecasts, along with the weather warnings active at a location.
Get Current Weather Forecast
Use the getCurrent method to retrieve the current weather forecast. Provide coordinates for the desired location, and the forecast will be returned through onComplete.
final locationCoordinates = Coordinates(
latitude: 48.864716,
longitude: 2.349014,
);
final weatherCurrentCompleter = Completer<List<LocationForecast>>();
WeatherService.getCurrent(
coords: [locationCoordinates],
onComplete: (err, result) async {
weatherCurrentCompleter.complete(result);
},
);
final currentForecast = await weatherCurrentCompleter.future;
showSnackbar("Forecast lenght list: ${currentForecast.length}");
Verify that LocationForecast contains Conditions and each Condition includes a Parameter. If data is unavailable for the specified location and time, the API may return empty lists.
The result contains as many LocationForecast objects as coordinates provided to the coords parameter.
Each returned LocationForecast also carries the weather warnings active at that location in its warnings list. See Warning for the hazard fields and for how to order overlapping warnings.
for (final forecast in currentForecast) {
for (final warning in forecast.warnings) {
showSnackbar('${warning.name} - ${warning.severity} until ${warning.endStamp}');
}
}
Get Hourly Weather Forecast
Use the getHourlyForecast method to retrieve hourly weather forecasts. Specify the number of hours and coordinates for the desired location.
final locationCoordinates = Coordinates(
latitude: 48.864716,
longitude: 2.349014,
);
final weatherHourlyCompleter = Completer<List<LocationForecast>>();
WeatherService.getHourlyForecast(
hours: 24,
coords: [locationCoordinates],
onComplete: (err, result) async {
weatherHourlyCompleter.complete(result);
},
);
final hourlyForecast = await weatherHourlyCompleter.future;
showSnackbar("Forecast lenght list: ${hourlyForecast.length}");
The number of requested hours must not exceed 240. Exceeding this limit results in an empty response and a GemError.outOfRange error.
Get Daily Weather Forecast
Use the getDailyForecast method to retrieve daily weather forecasts. Specify the number of days and coordinates for the desired location.
final locationCoordinates = Coordinates(
latitude: 48.864716,
longitude: 2.349014,
);
final weatherDailyCompleter = Completer<List<LocationForecast>>();
WeatherService.getDailyForecast(
days: 10,
coords: [locationCoordinates],
onComplete: (err, result) async {
weatherDailyCompleter.complete(result);
},
);
final dailyForecast = await weatherDailyCompleter.future;
showSnackbar("Forecast lenght list: ${dailyForecast.length}");
The number of requested days must not exceed 10. Exceeding this limit results in an empty response and a GemError.outOfRange error.
Get Weather Forecast with Duration
Use the getForecast method to retrieve weather forecasts for specific times and coordinates. Provide a list of WeatherDurationCoordinates, and onComplete returns as many LocationForecast objects as items in the list.
final weatherCompleter = Completer<List<LocationForecast>>();
WeatherService.getForecast(
coords: [
WeatherDurationCoordinates(
coordinates: Coordinates(
latitude: 48.864716,
longitude: 2.349014,
),
duration: Duration(days: 2),
)
],
onComplete: (err, result) async {
weatherCompleter.complete(result);
},
);
final forecast = await weatherCompleter.future;
showSnackbar("Forecast lenght list: ${forecast.length}");
The duration parameter in WeatherDurationCoordinates specifies the time offset into the future for the forecast.
Unlike the hourly and daily requests, getForecast also returns warnings. Each route point is evaluated against its own ETA: both the start and the end of the warning validity interval are applied against the time that point is reached.
Request warning coverage polygons
By default Warning.coverage is empty. Pass coverageGeometry: CoverageGeometry.polygons to getCurrent or getForecast to also receive the coverage polygons of each returned warning:
WeatherService.getCurrent(
coords: [locationCoordinates],
coverageGeometry: CoverageGeometry.polygons,
onComplete: (err, result) async {
weatherCurrentCompleter.complete(result);
},
);
| Enum Case | Description |
|---|---|
none | No coverage geometry. This is the default. |
polygons | Request the coverage polygons of the returned warnings. |
Coverage geometry can be large, running to thousands of points per warning. Request it only when the polygons will actually be drawn.
The flag controls only the geometry. The warnings themselves are always returned by getCurrent and getForecast, with or without it.
See Draw warning coverage for turning the returned polygons into map markers.