{"id":32519,"date":"2024-11-01T09:09:42","date_gmt":"2024-11-01T09:09:42","guid":{"rendered":"http:\/\/atmokpo.com\/w\/?p=32519"},"modified":"2024-11-01T11:54:58","modified_gmt":"2024-11-01T11:54:58","slug":"flutter-course-15-2-using-openweathermap","status":"publish","type":"post","link":"https:\/\/atmokpo.com\/w\/32519\/","title":{"rendered":"Flutter Course, 15.2 Using OpenWeatherMap"},"content":{"rendered":"<p>In this section, we will learn how to integrate the <strong>OpenWeatherMap API<\/strong> into a Flutter application. OpenWeatherMap provides various useful weather information, including real-time weather data, temperature, humidity, and wind speed. This API offers both free and paid plans, and today we will explain based on the free plan.<\/p>\n<h2>1. Sign up for OpenWeatherMap API and get the API key<\/h2>\n<p>The first step is to sign up on the OpenWeatherMap website to obtain an API key. Please follow the steps below:<\/p>\n<ol>\n<li>Go to the <a href=\"https:\/\/openweathermap.org\/\" target=\"_blank\" rel=\"noopener\">OpenWeatherMap website<\/a>.<\/li>\n<li>Click &#8220;Sign Up&#8221; in the top menu to create an account.<\/li>\n<li>Enter your email address and password, then complete the registration process.<\/li>\n<li>After logging in, navigate to the &#8220;API keys&#8221; menu to check your automatically generated default API key.<\/li>\n<li>You can create a new API key if needed.<\/li>\n<\/ol>\n<h2>2. Setting up the Flutter project<\/h2>\n<p>Now let&#8217;s set up the Flutter project. You can either create a new Flutter application or use an existing project.<\/p>\n<pre><code>flutter create weather_app<\/code><\/pre>\n<p>Navigate to the project directory:<\/p>\n<pre><code>cd weather_app<\/code><\/pre>\n<p>Next, add the required package to the pubspec.yaml file to send HTTP requests:<\/p>\n<pre><code>dependencies:\n  flutter:\n    sdk: flutter\n  http: ^0.13.3\n<\/code><\/pre>\n<p>Run the following command to install all dependencies:<\/p>\n<pre><code>flutter pub get<\/code><\/pre>\n<h2>3. Creating the weather data model<\/h2>\n<p>Create a Dart model class to learn the JSON data received from the OpenWeatherMap API. For example, let&#8217;s create a class for weather data.<\/p>\n<pre><code>class Weather {\n  final String city;\n  final double temperature;\n  final String description;\n\n  Weather({required this.city, required this.temperature, required this.description});\n\n  factory Weather.fromJson(Map<String, dynamic> json) {\n    return Weather(\n      city: json['name'],\n      temperature: json['main']['temp'] - 273.15, \/\/ Convert Kelvin to Celsius\n      description: json['weather'][0]['description'],\n    );\n  }\n}\n<\/code><\/pre>\n<h2>4. Fetching weather data with HTTP requests<\/h2>\n<p>Now it&#8217;s time to write the HTTP request to fetch weather information. We will send a GET request to the OpenWeatherMap API using the <strong>http<\/strong> package.<\/p>\n<p>Here\u2019s an example of writing a function to fetch weather information:<\/p>\n<pre><code>import 'dart:convert';\nimport 'package:http\/http.dart' as http;\n\nFuture<Weather> fetchWeather(String city) async {\n  final apiKey = 'YOUR_API_KEY_HERE'; \/\/ Enter your API key here\n  final response = await http.get(Uri.parse('https:\/\/api.openweathermap.org\/data\/2.5\/weather?q=$city&appid=$apiKey'));\n\n  if (response.statusCode == 200) {\n    return Weather.fromJson(json.decode(response.body));\n  } else {\n    throw Exception('Failed to load weather data');\n  }\n}\n<\/code><\/pre>\n<h2>5. Creating the user interface<\/h2>\n<p>Now let&#8217;s create a simple user interface to display the weather information. We will use Flutter components to show weather information based on the city name entered by the user.<\/p>\n<p>Here\u2019s a basic UI code example:<\/p>\n<pre><code>import 'package:flutter\/material.dart';\n\nclass WeatherApp extends StatelessWidget {\n  @override\n  Widget build(BuildContext context) {\n    return MaterialApp(\n      title: 'Weather App',\n      home: Scaffold(\n        appBar: AppBar(\n          title: Text('Weather Information'),\n        ),\n        body: WeatherInfo(),\n      ),\n    );\n  }\n}\n\nclass WeatherInfo extends StatefulWidget {\n  @override\n  _WeatherInfoState createState() => _WeatherInfoState();\n}\n\nclass _WeatherInfoState extends State<WeatherInfo> {\n  String city = '';\n  Weather? weather;\n\n  final TextEditingController controller = TextEditingController();\n\n  @override\n  Widget build(BuildContext context) {\n    return Column(\n      children: [\n        Padding(\n          padding: const EdgeInsets.all(16.0),\n          child: TextField(\n            controller: controller,\n            decoration: InputDecoration(labelText: 'Enter City Name'),\n          ),\n        ),\n        ElevatedButton(\n          onPressed: () {\n            setState(() {\n              city = controller.text.trim();\n            });\n            fetchWeather(city).then((value) {\n              setState(() {\n                weather = value;\n              });\n            }).catchError((error) {\n              showDialog(\n                context: context,\n                builder: (_) => AlertDialog(\n                  title: Text('Error'),\n                  content: Text(error.toString()),\n                  actions: [\n                    TextButton(\n                      onPressed: () {\n                        Navigator.of(context).pop();\n                      },\n                      child: Text('OK'),\n                    ),\n                  ],\n                ),\n              );\n            });\n          },\n          child: Text('Get Weather'),\n        ),\n        if (weather != null)\n          Padding(\n            padding: const EdgeInsets.all(16.0),\n            child: Text(\n              'City: ${weather!.city}\\nTemperature: ${weather!.temperature.toStringAsFixed(1)}\u00b0C\\nCondition: ${weather!.description}',\n              style: TextStyle(fontSize: 20),\n            ),\n          ),\n      ],\n    );\n  }\n}\n\nvoid main() {\n  runApp(WeatherApp());\n}\n<\/code><\/pre>\n<h2>6. Run the app and check the results<\/h2>\n<p>You can run the code written above on a device or emulator to check the results. Now, when you enter a city name in the input field and press the &#8220;Get Weather&#8221; button, it will display the weather information fetched through the API request on the screen.<\/p>\n<h2>7. Error handling and improvements<\/h2>\n<p>The currently implemented example provides only basic functionality, so there are various improvements that can be added. For example:<\/p>\n<ul>\n<li>Location-based weather information provision: Add a feature to automatically fetch weather information based on the user&#8217;s current location.<\/li>\n<li>Caching weather information: Add a caching mechanism to reduce response time for the same requests.<\/li>\n<li>Improving colors and design: Refine the UI design to enhance user experience.<\/li>\n<li>Providing various weather information: Display additional information such as humidity, wind speed, etc., besides temperature.<\/li>\n<\/ul>\n<h2>8. Conclusion<\/h2>\n<p>In this post, we have detailed how to fetch real-time weather information using Flutter and the OpenWeatherMap API. I hope this helps you learn how to use the API and add various features through practice. In the future, I encourage you to create many innovative applications using Flutter!<\/p>\n<footer>\n<p>\u00a9 2023 Flutter Course. All rights reserved.<\/p>\n<\/footer>\n","protected":false},"excerpt":{"rendered":"<p>In this section, we will learn how to integrate the OpenWeatherMap API into a Flutter application. OpenWeatherMap provides various useful weather information, including real-time weather data, temperature, humidity, and wind speed. This API offers both free and paid plans, and today we will explain based on the free plan. 1. Sign up for OpenWeatherMap API &hellip; <a href=\"https:\/\/atmokpo.com\/w\/32519\/\" class=\"more-link\">\ub354 \ubcf4\uae30<span class=\"screen-reader-text\"> &#8220;Flutter Course, 15.2 Using OpenWeatherMap&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[151],"tags":[],"class_list":["post-32519","post","type-post","status-publish","format-standard","hentry","category-flutter-course"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v26.2 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Flutter Course, 15.2 Using OpenWeatherMap - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/atmokpo.com\/w\/32519\/\" \/>\n<meta property=\"og:locale\" content=\"ko_KR\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Flutter Course, 15.2 Using OpenWeatherMap - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\" \/>\n<meta property=\"og:description\" content=\"In this section, we will learn how to integrate the OpenWeatherMap API into a Flutter application. OpenWeatherMap provides various useful weather information, including real-time weather data, temperature, humidity, and wind speed. This API offers both free and paid plans, and today we will explain based on the free plan. 1. Sign up for OpenWeatherMap API &hellip; \ub354 \ubcf4\uae30 &quot;Flutter Course, 15.2 Using OpenWeatherMap&quot;\" \/>\n<meta property=\"og:url\" content=\"https:\/\/atmokpo.com\/w\/32519\/\" \/>\n<meta property=\"og:site_name\" content=\"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\" \/>\n<meta property=\"article:published_time\" content=\"2024-11-01T09:09:42+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2024-11-01T11:54:58+00:00\" \/>\n<meta name=\"author\" content=\"root\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@bebubo4\" \/>\n<meta name=\"twitter:site\" content=\"@bebubo4\" \/>\n<meta name=\"twitter:label1\" content=\"\uae00\uc4f4\uc774\" \/>\n\t<meta name=\"twitter:data1\" content=\"root\" \/>\n\t<meta name=\"twitter:label2\" content=\"\uc608\uc0c1 \ub418\ub294 \ud310\ub3c5 \uc2dc\uac04\" \/>\n\t<meta name=\"twitter:data2\" content=\"4\ubd84\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/atmokpo.com\/w\/32519\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/atmokpo.com\/w\/32519\/\"},\"author\":{\"name\":\"root\",\"@id\":\"https:\/\/atmokpo.com\/w\/#\/schema\/person\/91b6b3b138fbba0efb4ae64b1abd81d7\"},\"headline\":\"Flutter Course, 15.2 Using OpenWeatherMap\",\"datePublished\":\"2024-11-01T09:09:42+00:00\",\"dateModified\":\"2024-11-01T11:54:58+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/atmokpo.com\/w\/32519\/\"},\"wordCount\":499,\"publisher\":{\"@id\":\"https:\/\/atmokpo.com\/w\/#organization\"},\"articleSection\":[\"Flutter course\"],\"inLanguage\":\"ko-KR\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/atmokpo.com\/w\/32519\/\",\"url\":\"https:\/\/atmokpo.com\/w\/32519\/\",\"name\":\"Flutter Course, 15.2 Using OpenWeatherMap - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\",\"isPartOf\":{\"@id\":\"https:\/\/atmokpo.com\/w\/#website\"},\"datePublished\":\"2024-11-01T09:09:42+00:00\",\"dateModified\":\"2024-11-01T11:54:58+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/atmokpo.com\/w\/32519\/#breadcrumb\"},\"inLanguage\":\"ko-KR\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/atmokpo.com\/w\/32519\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/atmokpo.com\/w\/32519\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\ud648\",\"item\":\"https:\/\/atmokpo.com\/w\/en\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Flutter Course, 15.2 Using OpenWeatherMap\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/atmokpo.com\/w\/#website\",\"url\":\"https:\/\/atmokpo.com\/w\/\",\"name\":\"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\",\"description\":\"\",\"publisher\":{\"@id\":\"https:\/\/atmokpo.com\/w\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/atmokpo.com\/w\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"ko-KR\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/atmokpo.com\/w\/#organization\",\"name\":\"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\",\"url\":\"https:\/\/atmokpo.com\/w\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"ko-KR\",\"@id\":\"https:\/\/atmokpo.com\/w\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/atmokpo.com\/w\/wp-content\/uploads\/2024\/11\/logo.png\",\"contentUrl\":\"https:\/\/atmokpo.com\/w\/wp-content\/uploads\/2024\/11\/logo.png\",\"width\":400,\"height\":400,\"caption\":\"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\"},\"image\":{\"@id\":\"https:\/\/atmokpo.com\/w\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/x.com\/bebubo4\"]},{\"@type\":\"Person\",\"@id\":\"https:\/\/atmokpo.com\/w\/#\/schema\/person\/91b6b3b138fbba0efb4ae64b1abd81d7\",\"name\":\"root\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"ko-KR\",\"@id\":\"https:\/\/atmokpo.com\/w\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/708197b41fc6435a7ce22d951b25d4a47e9e904270cb1f04682d4f025066f80c?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/708197b41fc6435a7ce22d951b25d4a47e9e904270cb1f04682d4f025066f80c?s=96&d=mm&r=g\",\"caption\":\"root\"},\"sameAs\":[\"http:\/\/atmokpo.com\/w\"],\"url\":\"https:\/\/atmokpo.com\/w\/author\/root\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Flutter Course, 15.2 Using OpenWeatherMap - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/atmokpo.com\/w\/32519\/","og_locale":"ko_KR","og_type":"article","og_title":"Flutter Course, 15.2 Using OpenWeatherMap - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","og_description":"In this section, we will learn how to integrate the OpenWeatherMap API into a Flutter application. OpenWeatherMap provides various useful weather information, including real-time weather data, temperature, humidity, and wind speed. This API offers both free and paid plans, and today we will explain based on the free plan. 1. Sign up for OpenWeatherMap API &hellip; \ub354 \ubcf4\uae30 \"Flutter Course, 15.2 Using OpenWeatherMap\"","og_url":"https:\/\/atmokpo.com\/w\/32519\/","og_site_name":"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","article_published_time":"2024-11-01T09:09:42+00:00","article_modified_time":"2024-11-01T11:54:58+00:00","author":"root","twitter_card":"summary_large_image","twitter_creator":"@bebubo4","twitter_site":"@bebubo4","twitter_misc":{"\uae00\uc4f4\uc774":"root","\uc608\uc0c1 \ub418\ub294 \ud310\ub3c5 \uc2dc\uac04":"4\ubd84"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/atmokpo.com\/w\/32519\/#article","isPartOf":{"@id":"https:\/\/atmokpo.com\/w\/32519\/"},"author":{"name":"root","@id":"https:\/\/atmokpo.com\/w\/#\/schema\/person\/91b6b3b138fbba0efb4ae64b1abd81d7"},"headline":"Flutter Course, 15.2 Using OpenWeatherMap","datePublished":"2024-11-01T09:09:42+00:00","dateModified":"2024-11-01T11:54:58+00:00","mainEntityOfPage":{"@id":"https:\/\/atmokpo.com\/w\/32519\/"},"wordCount":499,"publisher":{"@id":"https:\/\/atmokpo.com\/w\/#organization"},"articleSection":["Flutter course"],"inLanguage":"ko-KR"},{"@type":"WebPage","@id":"https:\/\/atmokpo.com\/w\/32519\/","url":"https:\/\/atmokpo.com\/w\/32519\/","name":"Flutter Course, 15.2 Using OpenWeatherMap - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","isPartOf":{"@id":"https:\/\/atmokpo.com\/w\/#website"},"datePublished":"2024-11-01T09:09:42+00:00","dateModified":"2024-11-01T11:54:58+00:00","breadcrumb":{"@id":"https:\/\/atmokpo.com\/w\/32519\/#breadcrumb"},"inLanguage":"ko-KR","potentialAction":[{"@type":"ReadAction","target":["https:\/\/atmokpo.com\/w\/32519\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/atmokpo.com\/w\/32519\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\ud648","item":"https:\/\/atmokpo.com\/w\/en\/"},{"@type":"ListItem","position":2,"name":"Flutter Course, 15.2 Using OpenWeatherMap"}]},{"@type":"WebSite","@id":"https:\/\/atmokpo.com\/w\/#website","url":"https:\/\/atmokpo.com\/w\/","name":"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","description":"","publisher":{"@id":"https:\/\/atmokpo.com\/w\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/atmokpo.com\/w\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"ko-KR"},{"@type":"Organization","@id":"https:\/\/atmokpo.com\/w\/#organization","name":"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","url":"https:\/\/atmokpo.com\/w\/","logo":{"@type":"ImageObject","inLanguage":"ko-KR","@id":"https:\/\/atmokpo.com\/w\/#\/schema\/logo\/image\/","url":"https:\/\/atmokpo.com\/w\/wp-content\/uploads\/2024\/11\/logo.png","contentUrl":"https:\/\/atmokpo.com\/w\/wp-content\/uploads\/2024\/11\/logo.png","width":400,"height":400,"caption":"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8"},"image":{"@id":"https:\/\/atmokpo.com\/w\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/x.com\/bebubo4"]},{"@type":"Person","@id":"https:\/\/atmokpo.com\/w\/#\/schema\/person\/91b6b3b138fbba0efb4ae64b1abd81d7","name":"root","image":{"@type":"ImageObject","inLanguage":"ko-KR","@id":"https:\/\/atmokpo.com\/w\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/708197b41fc6435a7ce22d951b25d4a47e9e904270cb1f04682d4f025066f80c?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/708197b41fc6435a7ce22d951b25d4a47e9e904270cb1f04682d4f025066f80c?s=96&d=mm&r=g","caption":"root"},"sameAs":["http:\/\/atmokpo.com\/w"],"url":"https:\/\/atmokpo.com\/w\/author\/root\/"}]}},"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack-related-posts":[],"_links":{"self":[{"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/posts\/32519","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/comments?post=32519"}],"version-history":[{"count":1,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/posts\/32519\/revisions"}],"predecessor-version":[{"id":32520,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/posts\/32519\/revisions\/32520"}],"wp:attachment":[{"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/media?parent=32519"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/categories?post=32519"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/tags?post=32519"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}