In today’s world, most applications heavily rely on fetching 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 fetch data from the internet follow the below steps:
Import the http package
Make a network request using the http package
Convert the response into custom Dart object
Display the data in a suitable format
Importing The http Package:
To install the http package use the below command in your command prompt:
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;
We can use the http.get() method to fetch the sample album data from JSONPlaceholder as shown below:
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:
Please Login to comment...