? weather_pack

一种快速获取天气状况的方法。


资源

  • folder example。有一个简单的示例展示了如何使用该包的基本功能,以及一个不使用 flutter 的控制台迷你应用程序

欢迎随时提出建议,以便将材料纳入此列表 ^_~

Endpoints openweathermap.org

让我们约定将 Openweathermap 称为 OWM

该库使用以下网站端点 openweathermap.org

端点或路径 使用此端点的类或方法 查看更多
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

目录

安装

  1. 将依赖项添加到您的 pubspec.yaml

    dependencies:
      weather_pack: <latest_version>
  2. 运行命令: flutter pub get
  3. 在您的代码中使用
    import 'package:weather_pack/weather_pack.dart';

入门

获取当前天气的最简单方法

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);
}

您还可以更改请求语言

final lang = WeatherLanguage.arabic;

final wService = WeatherService(api, language: lang);
支持的语言:(点击打开)
  1. 南非荷兰语
  2. 阿尔巴尼亚语
  3. 阿拉伯语
  4. 阿塞拜疆语
  5. 保加利亚语
  6. 加泰罗尼亚语
  7. 捷克语
  8. 丹麦语
  9. 德语
  10. 希腊语
  11. 英语
  12. 巴斯克语
  13. 波斯语
  14. 波斯语
  15. 芬兰语
  16. 法语
  17. 加利西亚语
  18. 希伯来语
  19. 印地语
  20. 克罗地亚语
  21. 匈牙利语
  22. 印度尼西亚语
  23. 意大利语
  24. 日语
  25. 韩语
  26. 拉脱维亚语
  27. 拉脱维亚语
  28. 马其顿语
  29. 挪威语
  30. 荷兰语
  31. 波兰语
  32. 葡萄牙语
  33. 巴西葡萄牙语
  34. 罗马尼亚语
  35. 俄语
  36. 瑞典语
  37. 斯洛伐克语
  38. 斯洛文尼亚语
  39. 西班牙语
  40. 塞尔维亚语
  41. 泰语
  42. 土耳其语
  43. 乌克兰语
  44. 越南语
  45. 简体中文
  46. 繁体中文
  47. 祖鲁语

根据 OWM 服务(查看更多

您可以使用 lang 参数以您的语言获取输出。

翻译应用于 city namedescription 字段。

使用天气服务

目前有两个天气模型——WeatherCurrentWeatherOneCall

WeatherOneCall 包括

  1. WeatherCurrent
  2. List<WeatherHourly>
  3. List<WeatherMinutely>
  4. List<WeatherDaily>
  5. List<WeatherAlert>

如何使用?

您可以通过以下方式获取天气

final WeatherCurrent current = await wService
    .currentWeatherByLocation(latitude: 52.374, longitude: 4.88969);

final WeatherOneCall onecall = await wService
    .oneCallWeatherByLocation(latitude: 52.374, longitude: 4.88969);

为什么你们只使用通过坐标搜索天气?

根据 OWM 网站

如果您需要自动将城市名称和邮政编码转换为地理坐标,反之亦然,请使用 Geocoder API。

请注意,内置的地理编码器已弃用。尽管它仍然可用,但该功能不再提供错误修复和更新。

使用地理编码服务

GeocodingService 是一个在处理地理名称和坐标时用于简便位置搜索的服务。支持正向和反向方法

  • 正向地理编码将指定的地点名称或邮政编码转换为精确的地理坐标;
  • 反向地理编码将地理坐标转换为附近地点的名称;

您可以在此链接上了解更多信息:Geocoding API OpenWeather

如何使用?

按以下方式创建 GeocodingService

final String cityName = 'suggested location name';
final String apiKey = 'your api key for OWM';

final GeocodingService gService = GeocodingService(apiKey);

要通过地名查找,请使用正向地理编码

final List<PlaceGeocode> places = await gService.getLocationByCityName(cityName);

或使用反向地理编码

final List<PlaceGeocode> places = await gService.getLocationByCoordinates(latitude: 52.374, longitude: 4.88969);

使用测量单位

默认情况下,所有天气模型,例如 WeatherCurrent,其可测量值类型为 double。要以方便的格式显示数据,需要使用转换方法 valuevalueToString

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`
}

总的来说,valueToString() 方法用于在 UI 中正确显示,而 value() 方法用于精确计算。

有几种测量单位

测量单位 支持的单位 转换
温度 温度 开尔文、摄氏度、华氏度 +
速度 速度 米/秒、英里/小时、公里/小时 +
压力 压力 百帕、毫巴、毫米汞柱、千帕、大气压、英寸汞柱 +
基点 SideOfTheWorld n、ne、e、se、s、sw、w、nw +(其他)

? 提示SideOfTheWorld 枚举包含一个静态方法 fromDegrees(),用于将度数转换为基点方向。

使用天气图标

您可以使用 OWM 服务提供的天气图标。有关天气状况的更多信息。

图标本地存储在此包的 assets/weather_icons/ 路径下。它们按照声明分辨率感知图像资源进行排序。这反映了以下对应关系

100*100 - in default(implied resolution @1)
200x200 - @2
300x300 - @3
400x400 - @4

同时保持图像质量。

如何使用?

安全地获取天气图标

Image getWeatherIcon(String weatherIcon) {
  return Image.asset(
    ImagePathWeather.getPathWeatherIcon(weatherIcon),
    filterQuality: FilterQuality.high, // optional
    package: ImagePathWeather.packageName,
  );
}

或者完全手动处理

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
  );
}

总的来说,您可以通过指定 @4 到路径来获得不受平台分辨率影响的最佳质量

'assets/weather_icons/@4/$weatherIcon.png'

API 密钥测试

可以测试 API 密钥。为此,OWMApiTest 类有一个方法 isCorrectApiKey()

void worksTestedAPIkey({
  String testedAPIkey = 'Your_key',
}) async {

  // If the key is correct, it will return `true`
  final bool isCorrect = await OWMApiTest().isCorrectApiKey(testedAPIkey);
}

正在开发中的功能

  1. 按地点名称获取天气(内置地理编码)。

附加信息

用 ❤️ 制作。尽情享受吧!

GitHub

查看 Github