In today’s world, most applications heavily rely on fetching and updating information from the servers through the internet. In Flutter, such services are provided by the http package. In this article, we will explore the same.
To update the data on the Internet follow the below steps:
- Import the http package
- Update data t using the http package
- Convert the response into custom Dart object
- Get the data from the internet.
- Update and display the response on screen
Importing The http Package:
To install the http package use the below command in your command prompt:
Or, if you’re 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;
Update Data over the Internet:
Use the http.put() method to update the title of the Album in JSONPlaceholder as shown below:
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, converting 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.Respone to an Album:
Now, follow the below steps to update the fetchAlbum() function to return a Future<Album>:
- Use the dart: convert package to convert the response body into a JSON Map.
- Use the fromJSON() factory method to convert JSON Map into Album if the server returns an OK response with a status code of 200.
- Throw an exception if the server doesn’t return an OK response with a status code of 200.
Fetching the Data:
Now use the fetch() method to fetch the data as shown below:
Update the existing Data through user input:
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:
Displaying the Data:
Use the FlutterBuilder widget to display the data on the screen as shown below:
Complete Source Code:
- Flutter - Fetching Data From the Internet
- Flutter - Deleting Data On The Internet
- Flutter - Sending Data To The Internet
- Retrieve Data From TextFields in Flutter
- Flutter - Send Data to Screen
- Flutter - Return Data from Screen
- Flutter - Send Data to Screen using RouteSettings
- Flutter - Read and Write Data on Firebase
- Current Internet Connection Type in Real-Time Programmatically in Android
- Getting Started with Cross-Platform Mobile Application using Flutter
- Difference between React Native and Flutter
- Difference Between Rows and Columns vs Container in Flutter
- Multi Page Applications in Flutter
- Routes and Navigator in Flutter
- Container class in Flutter
- Difference Between Stateless and Stateful Widget in Flutter
- Rive animations in Flutter
- ProgressIndicator in Flutter
- Flutter - Stateful vs Stateless Widgets
- Listview.builder in Flutter
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.