<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Sachin Darde on Medium]]></title>
        <description><![CDATA[Stories by Sachin Darde on Medium]]></description>
        <link>https://medium.com/@sachindarde3125?source=rss-494dd3aa0e31------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*XFoxtqB4VqyCQIGd</url>
            <title>Stories by Sachin Darde on Medium</title>
            <link>https://medium.com/@sachindarde3125?source=rss-494dd3aa0e31------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Fri, 20 Nov 2026 06:38:41 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@sachindarde3125/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Communication with the server (Http GET/POST requests)]]></title>
            <link>https://medium.com/@sachindarde3125/communication-with-the-server-http-get-post-requests-23e3badedd9d?source=rss-494dd3aa0e31------2</link>
            <guid isPermaLink="false">https://medium.com/p/23e3badedd9d</guid>
            <category><![CDATA[flutter]]></category>
            <category><![CDATA[tutorial]]></category>
            <category><![CDATA[http-request]]></category>
            <category><![CDATA[dart]]></category>
            <category><![CDATA[flutter-widget]]></category>
            <dc:creator><![CDATA[Sachin Darde]]></dc:creator>
            <pubDate>Thu, 16 Jul 2020 07:17:32 GMT</pubDate>
            <atom:updated>2020-07-16T07:17:32.563Z</atom:updated>
            <content:encoded><![CDATA[<p>Communication with server from a client application most essential part for every dynamic application. we often story limited data on a client device for security and integrity purpose.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*peOCRHebCS1waUvTDCw9lQ.jpeg" /></figure><h3>What is HTTP?</h3><p>The Hypertext Transfer Protocol (HTTP) is designed to enable communications between clients and servers. HTTP functions as a request-response protocol in the client-server computing model. A web browser, for example, maybe the client and an application running on a computer hosting a website may be the server.</p><h3>We will divide this tutorial into sections as follows:</h3><ol><li>Prerequisites</li><li>Introduction to http.dart package</li><li>Types of Http requests</li><li>Fetch data from the server (Http Get request)</li><li>Send data to the server (Http Post request)</li></ol><h3>1. Prerequisites</h3><p>Before working with the HTTP dart package, you should have a basic understanding of the following:</p><ul><li>Dart programming</li><li>Usage of the HTTP protocol</li><li>Flutter app-design fundamentals</li><li>Knowledge about asynchronously observable techniques such as Future, See the <a href="https://codeapprun.io/blog/communication-with-server-http-requests#">future</a> guide.</li></ul><h3>2. Introduction to http.dart package</h3><p>This package contains a set of high-level functions and classes that make it easy to consume HTTP resources. It’s platform-independent and can be used on both the command-line and the browser.The easiest way to use this library is via the top-level functions. They allow you to make individual HTTP requests with minimal hassle.</p><p>For installing this package to your project visit <a href="https://pub.dev/packages/http#-installing-tab-">Add package</a> and for using this package with <a href="https://codeapprun.io/">codeapprun.io</a> use <a href="https://storage.googleapis.com/s3.codeapprun.io/assets/video/Video6_1.mp4">marketplace</a>.</p><p>Okay, Let’s get started!</p><h3>3. Types of Http requests</h3><p>We can use a different method to communicate with a server, types and their usage as followed:</p><ul><li><strong>GET:</strong> The GET method is used to retrieve information from the given server using a given <a href="https://en.wikipedia.org/wiki/Uniform_Resource_Identifier">URI</a>.</li><li><strong>POST:</strong> A POST request is used to send data to the server, for example, customer information, file upload, etc.</li><li><strong>PUT:</strong> Replaces all current representations of the target resource with the uploaded content.</li><li><strong>DELETE:</strong> Removes all current representations of the target resource given by a <a href="https://en.wikipedia.org/wiki/Uniform_Resource_Identifier">URI</a>.</li><li><strong>PATCH:</strong> Use for making partial changes to an existing resource.</li><li><strong>Head:</strong> Same as GET, but transfers the status line and header section only.</li></ul><p>In this, Tutorial we will focus on GET and POST Requests</p><h3>4. Fetch data from the server (Http Get request)</h3><p>Get request involves the following steps:</p><ul><li>Making http get request</li><li>Decoding http.response into dynamic or dark object</li><li>Calling http request</li><li>Representing data</li><li>Interactive widget</li></ul><p>Making http get request</p><p>Assuming that you have imported Http.dart package into the current application. we will write a function named fetchAssetData() inside that function will call our get method with http.</p><p>For demonstration we will use <a href="https://codeapprun.io/">codeapprun.io</a> asset fetch get call :</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/789d50d3420ff079aed9711594356995/href">https://medium.com/media/789d50d3420ff079aed9711594356995/href</a></iframe><ul><li>A <a href="https://api.flutter.dev/flutter/dart-async/Future-class.html">Future</a> is used to represent a potential value, or error, that will be available at some time in the future. learn more about <a href="https://api.flutter.dev/flutter/dart-async/Future-class.html">Future class</a></li><li>In the above example, we can see that Future contains http.Response which hold response data from the given call.</li></ul><p>Decoding http.response into dynamic or dark object</p><p>Calling http request is quite simple, now to handle this response we convert http.Response into dart object and can also be used directly with dynamic .</p><p><strong>Converting into dart object</strong></p><p>First, create an Assets class that contains the data from the network request. It includes a factory constructor that creates an assets from JSON.</p><p>we will use JSON and Serialization to convert response into Dart Object</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/4fded987ddc969baec096ddae5c83446/href">https://medium.com/media/4fded987ddc969baec096ddae5c83446/href</a></iframe><p>Now that we have created an Assets class we will update our fetchAssetData data return Future object instead of Future :</p><ul><li>Convert the response body into a JSON Map with the dart:convert package.</li><li>Check for response status from Server, if response status is ok with status code 200, then we will convert JSON map into Assets object., or else we will throw an exception as shown in below code.</li></ul><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/af829b1d2925b208fdc27e06f2484569/href">https://medium.com/media/af829b1d2925b208fdc27e06f2484569/href</a></iframe><p><strong>Using as dynamic</strong></p><p>We can also use an object as a wildcard dynamic object, but working with a raw Future isn’t very convenient. generally, use in case of very few fields or in an application where we print response as it is.</p><p><strong>Calling Http request</strong></p><p>In this example, we are taking input from a user as a request query to call Assets search API. so we will call a function on click of Get Result button shown in following <a href="https://codeapprun.io/blog/communication-with-server-http-requests#">Interactive widget</a>.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/c62858ed51b3d16966acc2da91355aaf/href">https://medium.com/media/c62858ed51b3d16966acc2da91355aaf/href</a></iframe><p>This assetData is used in the next step.</p><p><strong>Representing data</strong></p><p>To represent the data on screen, use the <a href="https://api.flutter.dev/flutter/widgets/FutureBuilder-class.html">FutureBuilder</a> widget. The FutureBuilder widget comes with Flutter and makes it easy to work with asynchronous data sources.</p><p>FutureBuilder take two parameters as follow:</p><ul><li>The Future you want to work with. In this case, the future returned from the fetchAssetData() function.</li><li>A builder function that tells Flutter what to render, depending on the state of the Future: loading, success, or error.</li></ul><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/44925975dd4f10cbeb4c615a0aee1902/href">https://medium.com/media/44925975dd4f10cbeb4c615a0aee1902/href</a></iframe><p><strong>Note</strong>: snapshot.hasData only return true when it has some data in case we throw an exception we will get false.</p><p>Interactive widget</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fcodeapprun.io%2Fembed%2Fsachindarde%2FoYMhfCizRu%3Ftheme%3Dlight&amp;display_name=Codeapprun&amp;url=https%3A%2F%2Fcodeapprun.io%2Fsachindarde%2Fwidget%2FoYMhfCizRu%3Ftheme%3Dlight&amp;image=https%3A%2F%2Fstorage.googleapis.com%2Fs3.codeapprun.io%2Fwidget%2Fsachindarde%2FoYMhfCizRu%2Fmain.dart.png&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=codeapprun" width="900" height="600" frameborder="0" scrolling="no"><a href="https://medium.com/media/55a48e4c13c93f4db7c2e326c9703622/href">https://medium.com/media/55a48e4c13c93f4db7c2e326c9703622/href</a></iframe><h3>5. Send data to the server (Http Post request)</h3><p>Post request involves the following steps:</p><ul><li>Making http post request</li><li>Preparing header and body</li><li>Calling http request</li><li>Representing data</li><li>Interactive widget</li></ul><p>Making http post request</p><p>we will write a function named createData() inside that function will call our post method with http.</p><p>For demonstration we will use <a href="https://reqres.in/">reqres.in</a> create data post call :</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/67d8bfbcc5a0431a110e744da2a094b3/href">https://medium.com/media/67d8bfbcc5a0431a110e744da2a094b3/href</a></iframe><p>The Http.post() method returns Future with Response</p><ul><li>A <a href="https://api.flutter.dev/flutter/dart-async/Future-class.html">Future</a> is used to represent a potential value, or error, that will be available at some time in the future. learn more about <a href="https://api.flutter.dev/flutter/dart-async/Future-class.html">Future class</a></li><li>In the above example, we can see that Future contains http.Response which hold response data from the given call.</li></ul><p>Preparing header and body</p><p>With the help of jsonEncode we will convert map into request body for our http post request and in header parameter we will pass header, to know more about http header visit <a href="https://en.wikipedia.org/wiki/List_of_HTTP_header_fields">List of HTTP Header.</a></p><p>Following example show header and data passed as body { ‘foo1’ : ‘data’ } :</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/8cddb1739860a78a0152c4a7dd87f6a5/href">https://medium.com/media/8cddb1739860a78a0152c4a7dd87f6a5/href</a></iframe><p>After once we call createData() we will get a response from a server we will deal with it as follow:</p><ul><li>Convert the response body into a JSON Map with the dart:convert package.</li><li>Check for response status from Server, if response status is ok with status code 200, then we will convert JSON map into dynamic object., or else we will throw an exception as shown in the above code.</li></ul><p>Calling Http request</p><p>In this example, we are taking input from a user as a request query to call create data API. so we will call a function on click of Send data button shown in following <a href="https://codeapprun.io/blog/communication-with-server-http-requests#">Interactive widget</a>.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/56ab4e362ec7e106b7e6f44812fa699a/href">https://medium.com/media/56ab4e362ec7e106b7e6f44812fa699a/href</a></iframe><p>This responseData is used in the next step.</p><p>Representing data</p><p>To represent the data on screen, use the <a href="https://api.flutter.dev/flutter/widgets/FutureBuilder-class.html">FutureBuilder</a> widget. The FutureBuilder widget comes with Flutter and makes it easy to work with asynchronous data sources.</p><p>FutureBuilder take two parameters as follow:</p><ul><li>The Future you want to work with. In this case, the future returned from the createData() function.</li><li>A builder function that tells Flutter what to render, depending on the state of the Future: loading, success, or error.</li></ul><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/69376c0f35673dfb7c10939246725178/href">https://medium.com/media/69376c0f35673dfb7c10939246725178/href</a></iframe><p>Interactive widget</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fcodeapprun.io%2Fembed%2Fsachindarde%2FtEwAgyJxHm%3Ftheme%3Dlight&amp;display_name=Codeapprun&amp;url=https%3A%2F%2Fcodeapprun.io%2Fsachindarde%2Fwidget%2FtEwAgyJxHm%3Ftheme%3Dlight&amp;image=https%3A%2F%2Fstorage.googleapis.com%2Fs3.codeapprun.io%2Fwidget%2Fsachindarde%2FtEwAgyJxHm%2Fmain.dart.png&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=codeapprun" width="900" height="600" frameborder="0" scrolling="no"><a href="https://medium.com/media/7abbcfb4ca5e73e651eca9924608f476/href">https://medium.com/media/7abbcfb4ca5e73e651eca9924608f476/href</a></iframe><p>Thank you, visit <a href="https://codeapprun.io/">https://codeapprun.io/</a> for more flutter widgets!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=23e3badedd9d" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>