weather_pack 1.0.0
weather_pack: ^1.0.0 copied to clipboard
The project is designed to obtain weather via the OpenWeatherMap API. With geocoding and units measure. :)
[]
๐ฆ weather_pack #
A quick way to get access to weather conditions.
Why choose this library?
basic:
- ๐ฒ Easy to use - you only need the APIKEY.
- ๐ Built-in geocoding - search for locations by assumed names or by coordinates.
- ๐ฉบ Various units of measurement - speed, temperature, pressure and cardinal points.
- ๐ค There are original weather icons.
advanced: (Click to open)
- ๐ฎ At least one release application is already based on this package. Therefore, there is an additional guarantee of security and timely updates of this package.
- ๐ There is a method for checking your api for correctness.
- ๐งฑ It is very easy to customize data models. Create your own data models and take only what you need from the built-in ones.
- ๐งพ The code is well documented and each class is labeled and decoded. There are unit tests for the main functions of the package.
- ๐ฆบ Safe unpacking of types. If the server stops outputting values - your weather model will have a null field and the application will not crash.
- ๐ง This package has no unnecessary dependencies and contains minimal code. Also, all platforms are supported.
Endpoints openweathermap.org #
Let's agree to designate Openweathermap as OWM.
The library uses the following site endpoints openweathermap.org:
Endpoint or Path | A class or method that uses this endpoint | See more |
---|---|---|
api.openweathermap.org/data/2.5/weather | WeatherService.currentWeatherByLocation |
current |
api.openweathermap.org/data/2.5/onecall | WeatherService.oneCallWeatherByLocation |
one-call-api |
api.openweathermap.org/geo/1.0/direct | GeocodingService.getLocationByCityName |
geocoding-direct |
api.openweathermap.org/geo/1.0/reverse | GeocodingService.getLocationByCoordinates |
geocoding-reverse |
Table of Contents #
- ๐ฆ weather_pack
- Endpoints openweathermap.org
- Table of Contents
- Installing
- Getting Started
- Usage weather service
- Usage geocoding service
- Usage units measure
- Usage weather icons
- API key testing
- Resources
- Features in development
- Additional information
Installing #
- Add dependency to your
pubspec.yaml
:dependencies: weather_pack: ^<latest_version>
- Run the command:
flutter pub get
- Use in your code:
import 'package:weather_pack/weather_pack.dart';
Getting Started #
The easiest way to get the current weather:
Future<void> main() async {
const api = 'YOUR_APIKEY'; // TODO: change to your Openweathermap APIkey
final wService = WeatherService(api);
// get the current weather in Amsterdam
final WeatherCurrent currently = await wService.currentWeatherByLocation(
latitude: 52.374, longitude: 4.88969);
print(currently);
}
You can also change the request language:
final lang = WeatherLanguage.arabic;
final wService = WeatherService(api, language: lang);
Supported languages: (Click to open)
- Afrikaans
- Albanian
- Arabic
- Azerbaijani
- Bulgarian
- Catalan
- Czech
- Danish
- German
- Greek
- English
- Basque
- Persian
- Farsi
- Finnish
- French
- Galician
- Hebrew
- Hindi
- Croatian
- Hungarian
- Indonesian
- Italian
- Japanese
- Korean
- Latvian
- Latvian
- Macedonian
- Norwegian
- Dutch
- Polish
- Portuguese
- Portuguรชs Brasil
- Romanian
- Russian
- Swedish
- Slovak
- Slovenian
- Spanish
- Serbian
- Thai
- Turkish
- Ukrainian
- Vietnamese
- Chinese Simplified
- Chinese Traditional
- Zulu
According to OWM service (See more):
You can use the
lang
parameter to get the output in your language.Translation is applied for the
city name
anddescription
fields.
Usage weather service #
Now there are two weather models - WeatherCurrent
and WeatherOneCall
.
WeatherOneCall
includes:
WeatherCurrent
List<WeatherHourly>
List<WeatherMinutely>
List<WeatherDaily>
List<WeatherAlert>
How to use?
You can get the weather in the following way:
final WeatherCurrent current = await wService
.currentWeatherByLocation(latitude: 52.374, longitude: 4.88969);
final WeatherOneCall onecall = await wService
.oneCallWeatherByLocation(latitude: 52.374, longitude: 4.88969);
Why do you only use the weather search by coordinates?
According to the website OWM:
Please use Geocoder API if you need automatic convert city names and zip-codes to geo coordinates and the other way around.
Please note that built-in geocoder has been deprecated. Although it is still available for use, bug fixing and updates are no longer available for this functionality.
Usage geocoding service #
GeocodingService
is a service for easy location search when working with geographical names
and coordinates. Supports both the direct and reverse methods:
- Direct geocoding converts the specified name of a location or zip/post code into the exact geographical coordinates;
- Reverse geocoding converts the geographical coordinates into the names of the nearby locations;
You can find out more at this link: Geocoding API OpenWeather
How to use?
Create GeocodingService
in the following way:
final String cityName = 'suggested location name';
final String apiKey = 'your api key for OWM';
final GeocodingService gService = GeocodingService(apiKey);
To find using place names use direct geocoding:
final List<PlaceGeocode> places = await gService.getLocationByCityName(cityName);
or use reverse geocoding
final List<PlaceGeocode> places = await gService.getLocationByCoordinates(latitude: 52.374, longitude: 4.88969);
Usage units measure #
By default, all weather models, e.g. WeatherCurrent
, have measurable values of type double
.
To display the data in a convenient format, it is necessary use the conversion method value
or valueToString
:
void worksTempUnits({
double temp = 270.78, // ex. received from [WeatherCurrent.temp]
int precision = 3,
Temp unitsMeasure = Temp.celsius,
}) {
// The default temperature is measured in Kelvin of the `double` type.
// We need the temperature to be displayed in Celsius to 3 decimal places
print(unitsMeasure.value(temp, precision)); // `-2.37` type `double`
print(unitsMeasure.valueToString(temp, precision)); // `-2.370` type `String`
// if precision is 0:
print(unitsMeasure.value(temp, 0)); // `-2.0` type `double`
print(unitsMeasure.valueToString(temp, 0)); // `-2` type `String`
}
By and large, the valueToString()
method is needed to display correctly in ui,
and the value()
method is for accurate calculations.
There are several units of measurement:
Units of measure | Class | Supported units | Conversion |
---|---|---|---|
Temperature | Temp |
kelvin, celsius, fahrenheit | + |
Speed | Speed |
ms, mph, kph | + |
Pressure | Pressure |
hectoPa, mbar, mmHg, kPa, atm, inHg | + |
Cardinal points | SideOfTheWorld |
n, ne, e, se, s, sw, w, nw | +(another) |
๐ก Tip: The SideOfTheWorld
enum contains a static method fromDegrees()
for converting degrees
to cardinal directions.
Usage weather icons #
You can use weather icons provided by the OWM service. See more about weather conditions.
Icons are stored locally in this package at the path assets/weather_icons/
.
They are ordered according to Declaring resolution-aware image assets.
This reflects the following correspondences:
100*100 - in default(implied resolution @1)
200x200 - @2
300x300 - @3
400x400 - @4
with the preservation of image quality.
How to use?
Get the weather icon in a safe way:
Image getWeatherIcon(String weatherIcon) {
return Image.asset(
ImagePathWeather.getPathWeatherIcon(weatherIcon),
filterQuality: FilterQuality.high, // optional
package: ImagePathWeather.packageName,
);
}
or to process it completely by hand:
Widget getWeatherIcon(WeatherCurrent weather) {
return Image.asset(
'assets/weather_icons/${weather.weatherIcon}.png', // icon path
package: 'weather_pack', // name package
filterQuality: FilterQuality.high, // optional
errorBuilder: (c, e, s) => Text(e), // will return the widget in case of an error
);
}
By and large, you can use the best quality regardless of platform resolution by specifying @4
to path:
'assets/weather_icons/@4/$weatherIcon.png'
API key testing #
It is possible to test the API key.
To do this, the OWMApiTest
class has a method isCorrectApiKey()
:
void worksTestedAPIkey({
String testedAPIkey = 'Your_key',
}) async {
// If the key is correct, it will return `true`
final bool isCorrect = await OWMApiTest().isCorrectApiKey(testedAPIkey);
}
Resources #
- folder
example
. There is a simple example of how to use the basic functions of the package, as well as a console mini-application without using flutter
[]
Feel free to suggest materials for inclusion in this list ^_~
Features in development #
- โ ?Getting weather by location name (built-in geocoding)
- โ ?Ability to get icons directly from openweathermap service
- โ ?Designate all kinds of icons in a static way, e.g. via
enum
Additional information #
Made with โค๏ธ. Enjoy it!