VOOZH about

URL: https://www.geeksforgeeks.org/flutter/flutter-fetching-data-from-the-internet/

⇱ Flutter - Fetching Data From the Internet - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Flutter - Fetching Data From the Internet

Last Updated : 15 Jul, 2025

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 this concept.

Steps to Implement Data Fetching from the Internet

Step 1 : Create a new flutter application

Create a new Flutter application using the command Prompt. To create a new app, write the below command and run it.

flutter create Progress_flutter

To know more about it refer this article: Creating a Simple Application in Flutter

Step 2 : Adding the Dependency

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 terminal.

flutter pub get


Step 3 : Importing the Dependency.

Use the below line of code in the main.dart file, to import the shimmer dependency :

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


Step 4 : Follow below flow to fetch data from internet

-

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:


-

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

  1. Use the dart: convert package to convert the response body into a JSON Map.
  2. Use the fromJSON() factory method to convert JSON Map into Album if the server returns an OK response with a status code of 200.
  3. Throw an exception if the server doesn't return an OK response with a status code of 200.


-

Now use the fetch() method to fetch the data as shown below:


-

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


Complete Source Code


Output:



Comment
Article Tags:

Explore