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.yaml
file. - Locate the
dependencies
section in the file. - Add
weatherapi
as a dependency. You can specify the version, too.
dependencies:
flutter:
sdk: flutter
weatherapi: ^1.1.0
- After adding the dependency, save the
pubspec.yaml
file. - Run
flutter pub get
in 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. Then, import the library.
import 'package:weatherapi/weatherapi.dart';
Next, an instance of a WeatherRequest
must be created using the obtained API key.
WeatherRequest wr = WeatherRequest('YOUR_API_KEY');
Alternatively, you can also specify a language for the weather results.
WeatherRequest wr = 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 rw = await wr.getRealtimeWeatherByCityName(cityName);
WeatherRequest wr = WeatherRequest('YOUR_API_KEY');
double latitude = 44.8;
double longitude = 10.33;
RealtimeWeather rw = await wr.getRealtimeWeatherByLocation(latitude, longitude);
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 fw = await wr.getForecastWeatherByCityName(cityName);
WeatherRequest wr = WeatherRequest('YOUR_API_KEY');
double latitude = 44.8;
double longitude = 10.33;
ForecastWeather fw = await wr.getForecastWeatherByLocation(latitude, longitude);
Search/Autocomplete API
For specific documentation on the Search/Autocomplete API, see the WeatherAPI docs.
Search/Autocomplete 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 sr = await wr.getResultsByCityName(cityName);
for (LocationResultData location in sr.locations) { /* ... */ }
WeatherRequest wr = WeatherRequest('YOUR_API_KEY');
double latitude = 44.8;
double longitude = 10.33;
SearchResults sr = await wr.getResultsByLocation(latitude, longitude);
for (LocationResultData location in sr.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:
arabic
bengali
bulgarian
chineseSimplified
chineseTraditional
czech
danish
dutch
finnish
french
german
greek
hindi
hungarian
italian
japanese
javanese
korean
mandarin
marathi
polish
portuguese
punjabi
romanian
russian
serbian
sinhalese
slovak
spanish
swedish
tamil
telugu
turkish
ukrainian
urdu
vietnamese
wuShanghainese
xiang
yueCantonese
zulu
The default language is English.
Libraries
- weatherapi
- A plugin for fetching weather forecasting via WeatherAPI.com.