![]() |
VOOZH | about |
In app development, the ability to fetch data from external sources, such as REST APIs, is a fundamental requirement. In Flutter, Whether you need to fetch data from a RESTful API, access a database, or retrieve content from a web server, Flutter provides you with the tools and packages(HTTP) to do this kind of API calls easily. Here we will explore how to interact with external data sources, retrieve JSON data, and integrate it into your Flutter application.
To build this app, you need the following items installed on your machine:
This is a joke API that gives a Random joke in each API call.
API Request method:
GET - https://api.chucknorris.io/jokes/randomJSON Data Format (Sample):
{
"categories": [],
"created_at": "2020-01-05 13:42:28.984661",
"icon_url": "https://assets.chucknorris.host/img/avatar/chuck-norris.png",
"id": "plj3-pqiRWuomVQKAy_AwQ",
"updated_at": "2020-01-05 13:42:28.984661",
"url": "https://api.chucknorris.io/jokes/plj3-pqiRWuomVQKAy_AwQ",
"value": "Chuck Norris can have his cake, eat it, then roundhouse kick you in the face with the extra power it gave him."
}
To set up Flutter Development on Android Studio please refer to Android Studio Setup for Flutter Development, and then create a new project in Android Studio please refer to Creating a Simple Application in Flutter.
In the pubspec.yaml file we have to add the below dependency for the http package which provide varrious API call methods.
dependencies:
flutter:
sdk: flutter
cupertino_icons: ^1.0.2
http: ^1.1.0
First of all import material.dart file.
import 'package:flutter/material.dart';
Here we are going to create a DataModel class to retrieve our jokes from the API.
Here the execution of our app starts.
In this class we are going to implement the MaterialApp , here we are also set the Theme of our App.
In this class we are going to make HTTP GET request to the API endpoint and fetch the data from it.
Here is the full Code of main.dart file