LoginSignup
1
3

More than 1 year has passed since last update.

Flutterで天気を取得するAPI

Last updated at Posted at 2022-07-19

天気を取得するAPIを発見したので紹介します。

今回使用するのはweatherというパッケージ。
早速パッケージをpubspec.ymlに追加していきます。

dependencies:
  weather: ^2.0.1

パッケージを追加したらOpenWeatherMapにサインインし、API keyを取得します。
情報を取得したい場所の緯度と経度を指定することで、結果が得られます。
緯度と経度の他にも、地名を指定する方法もあるようです。詳しくはDart公式ページにて

main.dart
import 'package:weather/weather.dart';
...
void getWeather() async {
  String key = "Your API key"
  double lat = 55.0111; //latitude(緯度)
  double lon = 15.0569; //longitude(経度)
  WeatherFactory wf = new WeatherFactory(key);
  
  Weather w = await wf.currentWeatherByLocation(lat, lon);
  
  print(w);
}

以下の結果が得られました。

flutter:     Place Name: Nexø [DK] (55.0111, 15.0569)
    Date: 2022-07-20 02:38:43.000
    Weather: Clouds, scattered clouds
    Temp: 21.0 Celsius, Temp (min): 21.0 Celsius, Temp (max): 21.0 Celsius,  Temp (feels like): 21.0 Celsius
    Sunrise: 2022-07-19 11:46:25.000, Sunset: 2022-07-20 04:25:18.000
    Wind: speed 0.51, degree: 260.0, gust null
    Weather Condition code: 802
1
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
3