Adel Blog
Elwannas HiriElwannas Hiri
Sep 09, 2022

Flutter – Sending Data To The Internet

Interacting with the Internet is crucial for most apps to function. In Flutter the http package is used to send the data to the internet. In this article, we will explore the same topic in detail. To send data to the internet through your application follow the below steps:

Importing The http Package:

To install the http package use the below command in your command prompt:

pub get

or, if you are using the flutter cmd use the below command:

flutter pub get

After the installation add the dependency to the pubsec.yml file as shown below:

import 'package:http/http.dart' as http;

Send Data to the Server:

In this article, we will create an Album data and send it to through the http.post() method.

Converting the Response:

Though making a network request is no big deal, working with the raw response data can be inconvenient. To make your life easier, convert the raw data (ie, http.response) into dart object. Here we will create an Album class that contains the JSON data as shown below:

   

Convert http.Response to an Album:

 

Now, follow the below steps to update the fetchAlbum() function to return a Future<Album>:

 

 

Ask User For an Album Title:

Now create a TextField  for the user to enter a title and a RaisedButton to send data to the server. Also, define a TextEditingController to read the user input from a TextField as shown below:

Display the Response:

Use the FlutterBuilder widget to display the data on the screen as shown below:

Complete Source Code:

Elwannas Hiri

Elwannas Hiri

Mobile app developer

Leave a Reply

Related Posts

Categories