![]() |
VOOZH | about |
Interacting with the Internet is crucial for most apps to function. In Flutter, sending data to the internet is one of them, and the http package is used to send the data to the internet. In this article, we will explore the same topic in detail.
Create a new Flutter application using the command Prompt. To create a new app, write the below command and run it.
flutter create app_nameTo know more about it refer this article: Creating a Simple Application in Flutter
To add the dependency to the pubspec.yaml file, add http as a dependency in the dependencies part of the pubspec.yaml file, as shown below:
Now run the below command in the terminal.
flutter pub getUse the below line of code in the main.dart file, to import the shimmer dependency :
import 'package:http/http.dart' as http;-
In this article, we will create an Album data and send it to JSONPlaceholder through the http.post() method.
-
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:
-
Now, follow the below steps to update the fetchAlbum() function to return a Future<Album>:
-
Now create a TextField for the user to enter a title and a ElevatedButton to send data to the server. Also, define a TextEditingController to read the user input from a TextField as shown below:
To know more about ElevatedButton in flutter refer this article: Flutter – ElevatedButton Widget
-
Use the FlutterBuilder widget to display the data on the screen as shown below:
main.dart:
Output: