weatherapi 1.0.1 weatherapi: ^1.0.1 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.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.0.1
- 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.
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/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 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:
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.