{"id":32509,"date":"2024-11-01T09:09:37","date_gmt":"2024-11-01T09:09:37","guid":{"rendered":"http:\/\/atmokpo.com\/w\/?p=32509"},"modified":"2024-11-01T11:55:00","modified_gmt":"2024-11-01T11:55:00","slug":"flutter-course-14-6-outputting-future-in-the-app","status":"publish","type":"post","link":"https:\/\/atmokpo.com\/w\/32509\/","title":{"rendered":"Flutter Course: 14.6 Outputting Future in the App"},"content":{"rendered":"<p><body><\/p>\n<p>\n        In this course, we will explain in detail how to create and output <code>Future<\/code> in Flutter. <code>Future<\/code> is a core concept in Dart&#8217;s asynchronous programming, representing an object that returns a value or error when an asynchronous task is completed. This allows us to perform time-consuming tasks without blocking the UI.\n    <\/p>\n<h2>1. What is Future?<\/h2>\n<p>\n<code>Future<\/code> is an object that represents a future result in asynchronous programming, allowing you to wait for the asynchronous task to complete and receive the result. For example, when fetching data through an HTTP request, you can proceed with other UI tasks without waiting for the request to finish.\n    <\/p>\n<h3>1.1 States of Future<\/h3>\n<ul>\n<li><strong>Pending:<\/strong> State where the task has not yet completed.<\/li>\n<li><strong>Completed:<\/strong> State where the task has completed and returned a result.<\/li>\n<li><strong>Error:<\/strong> State where an error occurred during the task.<\/li>\n<\/ul>\n<h2>2. Creating a Future<\/h2>\n<p>\nThere are two main ways to create a <code>Future<\/code>. The first is to use built-in methods, and the second is to create it through custom functions.\n    <\/p>\n<h3>2.1 Using Built-in Methods<\/h3>\n<p>\nYou can create a <code>Future<\/code> object that completes after a certain time using the <code>Future.delayed<\/code> method. The following is an example that returns a message after 2 seconds.\n    <\/p>\n<pre><code>Future<string> fetchData() {\n        return Future.delayed(Duration(seconds: 2), () {\n            return 'Data loading complete';\n        });\n    }<\/string><\/code><\/pre>\n<h3>2.2 Creating Custom Functions<\/h3>\n<p>\nYou can directly create a function that returns a <code>Future<\/code> to handle database or API requests. For example, let&#8217;s create a function to fetch user information from an API.\n    <\/p>\n<pre><code>Future<user> fetchUser(int userId) async {\n        final response = await http.get('https:\/\/api.example.com\/user\/$userId');\n        if (response.statusCode == 200) {\n            return User.fromJson(json.decode(response.body));\n        } else {\n            throw Exception('Failed to load user');\n        }\n    }<\/user><\/code><\/pre>\n<h2>3. Outputting Futures<\/h2>\n<p>\n        Now, let&#8217;s look at how to output the values of the <code>Future<\/code> object we created in a Flutter app. To do this, we use the <code>FutureBuilder<\/code> widget. The <code>FutureBuilder<\/code> dynamically updates the UI based on the state of the <code>Future<\/code>.\n    <\/p>\n<h3>3.1 Using FutureBuilder<\/h3>\n<p>\nTo use <code>FutureBuilder<\/code>, you need to define the <code>future<\/code> and <code>builder<\/code> parameters. You specify the <code>Future<\/code> object that performs the asynchronous task in <code>future<\/code>, and define the function that builds the UI based on the state of the asynchronous task in <code>builder<\/code>.\n    <\/p>\n<pre><code>class UserProfile extends StatelessWidget {\n        final int userId;\n        UserProfile(this.userId);\n        \n        @override\n        Widget build(BuildContext context) {\n            return FutureBuilder<user>(\n                future: fetchUser(userId),\n                builder: (context, snapshot) {\n                    if (snapshot.connectionState == ConnectionState.waiting) {\n                        return CircularProgressIndicator();\n                    } else if (snapshot.hasError) {\n                        return Text('Error: ${snapshot.error}');\n                    } else {\n                        return Text('User Name: ${snapshot.data.name}');\n                    }\n                },\n            );\n        }\n    }<\/user><\/code><\/pre>\n<h3>3.2 Exception Handling<\/h3>\n<p>\n        Since errors may occur during asynchronous tasks, it&#8217;s important to handle errors in <code>FutureBuilder<\/code> using <code>snapshot.hasError<\/code>. Proper exception handling can provide clear feedback to users.\n    <\/p>\n<h2>4. Complete Code Example<\/h2>\n<p>\n        Based on what we have learned so far, let&#8217;s look at a complete example. This example is a Flutter app that calls an API to fetch user information and displays it on the screen.\n    <\/p>\n<pre><code>import 'package:flutter\/material.dart';\nimport 'dart:convert';\nimport 'package:http\/http.dart' as http;\n\nclass User {\n    final String name;\n    \n    User({required this.name});\n    \n    factory User.fromJson(Map<string, dynamic=\"\"> json) {\n        return User(name: json['name']);\n    }\n}\n\nFuture<user> fetchUser(int userId) async {\n    final response = await http.get('https:\/\/api.example.com\/user\/$userId');\n    if (response.statusCode == 200) {\n        return User.fromJson(json.decode(response.body));\n    } else {\n        throw Exception('Failed to load user');\n    }\n}\n\nclass UserProfile extends StatelessWidget {\n    final int userId;\n    UserProfile(this.userId);\n    \n    @override\n    Widget build(BuildContext context) {\n        return FutureBuilder<user>(\n            future: fetchUser(userId),\n            builder: (context, snapshot) {\n                if (snapshot.connectionState == ConnectionState.waiting) {\n                    return CircularProgressIndicator();\n                } else if (snapshot.hasError) {\n                    return Text('Error: ${snapshot.error}');\n                } else {\n                    return Text('User Name: ${snapshot.data.name}');\n                }\n            },\n        );\n    }\n}\n\nvoid main() =&gt; runApp(MaterialApp(home: Scaffold(body: UserProfile(1))));\n<\/user><\/user><\/string,><\/code><\/pre>\n<h2>5. Conclusion<\/h2>\n<p>\n        In this course, we learned how to create <code>Future<\/code> in Flutter and efficiently handle asynchronous operations using it. The UI composed with <code>FutureBuilder<\/code> can change dynamically based on data loading states, which is very useful. These asynchronous programming techniques greatly help improve user experience even in complex apps.\n    <\/p>\n<div class=\"note\">\n<strong>Tip: <\/strong> If you want to delve deeper into asynchronous programming, also study the concepts of <code>Stream<\/code> and <code>async\/await<\/code>.\n    <\/div>\n<p><\/body><\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this course, we will explain in detail how to create and output Future in Flutter. Future is a core concept in Dart&#8217;s asynchronous programming, representing an object that returns a value or error when an asynchronous task is completed. This allows us to perform time-consuming tasks without blocking the UI. 1. What is Future? &hellip; <a href=\"https:\/\/atmokpo.com\/w\/32509\/\" class=\"more-link\">\ub354 \ubcf4\uae30<span class=\"screen-reader-text\"> &#8220;Flutter Course: 14.6 Outputting Future in the App&#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-32509","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: 14.6 Outputting Future in the App - \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\/32509\/\" \/>\n<meta property=\"og:locale\" content=\"ko_KR\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Flutter Course: 14.6 Outputting Future in the App - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\" \/>\n<meta property=\"og:description\" content=\"In this course, we will explain in detail how to create and output Future in Flutter. Future is a core concept in Dart&#8217;s asynchronous programming, representing an object that returns a value or error when an asynchronous task is completed. This allows us to perform time-consuming tasks without blocking the UI. 1. What is Future? &hellip; \ub354 \ubcf4\uae30 &quot;Flutter Course: 14.6 Outputting Future in the App&quot;\" \/>\n<meta property=\"og:url\" content=\"https:\/\/atmokpo.com\/w\/32509\/\" \/>\n<meta property=\"og:site_name\" content=\"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\" \/>\n<meta property=\"article:published_time\" content=\"2024-11-01T09:09:37+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2024-11-01T11:55:00+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=\"3\ubd84\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/atmokpo.com\/w\/32509\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/atmokpo.com\/w\/32509\/\"},\"author\":{\"name\":\"root\",\"@id\":\"https:\/\/atmokpo.com\/w\/#\/schema\/person\/91b6b3b138fbba0efb4ae64b1abd81d7\"},\"headline\":\"Flutter Course: 14.6 Outputting Future in the App\",\"datePublished\":\"2024-11-01T09:09:37+00:00\",\"dateModified\":\"2024-11-01T11:55:00+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/atmokpo.com\/w\/32509\/\"},\"wordCount\":432,\"publisher\":{\"@id\":\"https:\/\/atmokpo.com\/w\/#organization\"},\"articleSection\":[\"Flutter course\"],\"inLanguage\":\"ko-KR\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/atmokpo.com\/w\/32509\/\",\"url\":\"https:\/\/atmokpo.com\/w\/32509\/\",\"name\":\"Flutter Course: 14.6 Outputting Future in the App - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\",\"isPartOf\":{\"@id\":\"https:\/\/atmokpo.com\/w\/#website\"},\"datePublished\":\"2024-11-01T09:09:37+00:00\",\"dateModified\":\"2024-11-01T11:55:00+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/atmokpo.com\/w\/32509\/#breadcrumb\"},\"inLanguage\":\"ko-KR\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/atmokpo.com\/w\/32509\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/atmokpo.com\/w\/32509\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\ud648\",\"item\":\"https:\/\/atmokpo.com\/w\/en\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Flutter Course: 14.6 Outputting Future in the App\"}]},{\"@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: 14.6 Outputting Future in the App - \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\/32509\/","og_locale":"ko_KR","og_type":"article","og_title":"Flutter Course: 14.6 Outputting Future in the App - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","og_description":"In this course, we will explain in detail how to create and output Future in Flutter. Future is a core concept in Dart&#8217;s asynchronous programming, representing an object that returns a value or error when an asynchronous task is completed. This allows us to perform time-consuming tasks without blocking the UI. 1. What is Future? &hellip; \ub354 \ubcf4\uae30 \"Flutter Course: 14.6 Outputting Future in the App\"","og_url":"https:\/\/atmokpo.com\/w\/32509\/","og_site_name":"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","article_published_time":"2024-11-01T09:09:37+00:00","article_modified_time":"2024-11-01T11:55:00+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":"3\ubd84"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/atmokpo.com\/w\/32509\/#article","isPartOf":{"@id":"https:\/\/atmokpo.com\/w\/32509\/"},"author":{"name":"root","@id":"https:\/\/atmokpo.com\/w\/#\/schema\/person\/91b6b3b138fbba0efb4ae64b1abd81d7"},"headline":"Flutter Course: 14.6 Outputting Future in the App","datePublished":"2024-11-01T09:09:37+00:00","dateModified":"2024-11-01T11:55:00+00:00","mainEntityOfPage":{"@id":"https:\/\/atmokpo.com\/w\/32509\/"},"wordCount":432,"publisher":{"@id":"https:\/\/atmokpo.com\/w\/#organization"},"articleSection":["Flutter course"],"inLanguage":"ko-KR"},{"@type":"WebPage","@id":"https:\/\/atmokpo.com\/w\/32509\/","url":"https:\/\/atmokpo.com\/w\/32509\/","name":"Flutter Course: 14.6 Outputting Future in the App - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","isPartOf":{"@id":"https:\/\/atmokpo.com\/w\/#website"},"datePublished":"2024-11-01T09:09:37+00:00","dateModified":"2024-11-01T11:55:00+00:00","breadcrumb":{"@id":"https:\/\/atmokpo.com\/w\/32509\/#breadcrumb"},"inLanguage":"ko-KR","potentialAction":[{"@type":"ReadAction","target":["https:\/\/atmokpo.com\/w\/32509\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/atmokpo.com\/w\/32509\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\ud648","item":"https:\/\/atmokpo.com\/w\/en\/"},{"@type":"ListItem","position":2,"name":"Flutter Course: 14.6 Outputting Future in the App"}]},{"@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\/32509","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=32509"}],"version-history":[{"count":1,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/posts\/32509\/revisions"}],"predecessor-version":[{"id":32510,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/posts\/32509\/revisions\/32510"}],"wp:attachment":[{"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/media?parent=32509"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/categories?post=32509"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/tags?post=32509"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}