This package uses the OpenWeatherMAP API to get the current weather status as well as weather forecasts.
The weather can currently be fetched by providing a geolocation or a city name.
Add weather
as a dependency in pubspec.yaml
.
For help on adding as a dependency, view the pubspec documenation.
No permissions are needed for this package in isolatation, however for getting the device's geolocation we recommend using the geolocator plugin.
First and foremost you need an API key from OpenWeatherMap, which can be acquired for free here.
Next, an instance of a `WeatherFactory is created using the API key.
import 'package:weather/weather.dart';
...
WeatherFactory wf = new WeatherFactory("YOUR_API_KEY");
For specific documentation on the current weather API, see the OpenWeatherMap weather API docs.
The current weather is queried either through a latitude and longitude or through a city name, i.e.
double lat = 55.0111;
double lon = 15.0569;
String key = '856822fd8e22db5e1ba48c0e7d69844a';
String cityName = 'Kongens Lyngby';
WeatherFactory wf = WeatherFactory(key);
Through geolocation:
Weather w = await wf.currentWeatherByLocation(lat, lon);
Through city name:
Weather w = await wf.currentWeatherByCityName(cityName);
Example output:
Place Name: Kongens Lyngby [DK] (55.77, 12.5)
Date: 2020-07-13 17:17:34.000
Weather: Clouds, broken clouds
Temp: 17.1 Celsius, Temp (min): 16.7 Celsius, Temp (max): 18.0 Celsius, Temp (feels like): 13.4 Celsius
Sunrise: 2020-07-13 04:43:53.000, Sunset: 2020-07-13 21:47:15.000
Weather Condition code: 803
For a complete list of all the properties of the Weather class, check the documentation
The Temperature class holds a temperature and can output the temperature in the following units:
- Celsius
- Fahrenheit
- Kelvin
This can be done as given a Weather
object w
double celsius = w.temperature.celsius;
double fahrenheit = w.temperature.celsius;
double kelvin = w.temperature.kelvin;
For API documentation on the forecast API, see the OpenWeatherMap forecast API docs.
The forecast is a 5-day prediction and contains a list of Weather objects with 3 hours between them.
The forecast can also be fetched via geolocation or city name.
Via geolocation
List<Weather> forecast = await wf.fiveDayForecastByLocation(lat, lon);
Via city name
List<Weather> forecast = await wf.fiveDayForecastByCityName(cityName);
The following are cases for which an exception will be thrown:
- The provided OpenWeather API key is invalid
- An bad response was given by the API; it may be down.
Only for Android API level 28
Update the contents of the android/gradle.properties
file with the following:
android.enableJetifier=true
android.useAndroidX=true
org.gradle.jvmargs=-Xmx1536M
Next, add the following dependencies to your android/build.gradle
file:
dependencies {
classpath 'com.android.tools.build:gradle:3.3.0'
classpath 'com.google.gms:google-services:4.2.0'
}
And finally, set the Android compile- and minimum SDK versions to compileSdkVersion 28
, and minSdkVersion 21
respectively, inside the android/app/build.gradle
file.