weatherapi 1.0.0
weatherapi: ^1.0.0 copied to clipboard
A plugin for fetching weather forecasting via WeatherAPI.com. Works for both Android and iOS.
weatherapi #
This package uses the WeatherAPI.com API to get weather information.
You can retrieve the weather data by supplying either geographical coordinates or the name of a city.
Installation (Flutter) #
Add the package to your Flutter project by following these steps:
- Open your project's
pubspec.yamlfile. - Locate the
dependenciessection in the file. - Add
weatherapias a dependency. You can specify the version, too.
dependencies:
flutter:
sdk: flutter
weatherapi: ^1.0.0
- After adding the dependency, save the
pubspec.yamlfile. - Run
flutter pub getin your terminal or use the relevant option in your IDE to fetch the new dependency.
For help on adding dependencies, view the pubspec documenation.
Permissions #
This package does not require any permissions. However, if you intend to retrieve the device's geolocation, it is recommended to use the geolocator plugin.
Usage #
First you need an API key from WeatherAPI.com, which can be acquired for free here.
Next, an instance of a WeatherRequest must be created using the obtained API key.
import 'package:weatherapi/weatherapi.dart';
...
WeatherRequest wr = new WeatherRequest("YOUR_API_KEY");
Alternatively, you can also specify a language for the weather results.
WeatherRequest wr = new WeatherRequest("YOUR_API_KEY", language: Language.italian);
For all the supported languages, see the Languages section.
Realtime API (current weather) #
For specific documentation on the Realtime API, see the WeatherAPI docs.
Realtime weather API allows a user to get up to date current weather information. The data is returned as a RealtimeWeather object.
The current weather can be queried either through a city name or through a latitude and longitude.
WeatherRequest wr = WeatherRequest("YOUR_API_KEY");
String cityName = 'Parma';
RealtimeWeather w = await wr.currentWeatherByCityName(cityName);
WeatherRequest wr = WeatherRequest("YOUR_API_KEY");
double latitude = 44.8;
double longitude = 10.33;
RealtimeWeather w = await wr.currentWeatherByLocation(lat, lon);
Forecast API #
For specific documentation on the Forecast API, see the WeatherAPI docs.
Forecast weather API allows a user to get up to date current weather forecast. The data is returned as a ForecastWeather object.
The forecast weather can be queried either through a city name or a through latitude and longitude.
WeatherRequest wr = WeatherRequest("YOUR_API_KEY");
String cityName = 'Parma';
ForecastWeather f = await wr.forecastWeatherByCityName(cityName);
WeatherRequest wr = WeatherRequest("YOUR_API_KEY");
double latitude = 44.8;
double longitude = 10.33;
ForecastWeather f = await wr.forecastWeatherByLocation(lat, lon);
Search API #
For specific documentation on the Search API, see the WeatherAPI docs.
Search API allows a user to get a list of locations matching a provided search query. The data is returned as a SearchResults object.
The results can be queried either through a city name or through a latitude and longitude.
WeatherRequest wr = WeatherRequest("YOUR_API_KEY");
String cityName = 'Parma';
SearchResults lr = await wr.getResultsByCityName(cityName);
for (LocationResultData location in lr.locations) { /* ... */ }
WeatherRequest wr = WeatherRequest("YOUR_API_KEY");
double latitude = 44.8;
double longitude = 10.33;
SearchResults lr = await wr.getResultsByLocation(lat, lon);
for (LocationResultData location in lr.locations) { /* ... */ }
Exceptions #
An exception will be thrown in the following cases:
- The provided WeatherAPI.com key is invalid.
- A bad response was given by the API.
Languages #
The supported languages are as follows:
arabicbengalibulgarianchineseSimplifiedchineseTraditionalczechdanishdutchfinnishfrenchgermangreekhindihungarianitalianjapanesejavanesekoreanmandarinmarathipolishportuguesepunjabiromanianrussianserbiansinhaleseslovakspanishswedishtamilteluguturkishukrainianurduvietnamesewuShanghainesexiangyueCantonesezulu
The default language is English.