Free Ebook cover How to create apps from scratch to advanced using Flutter and Dart complete course

How to create apps from scratch to advanced using Flutter and Dart complete course

5

(4)

267 pages

Dart Basics: Consuming APIs

Capítulo 33

Estimated reading time: 4 minutes

Audio Icon

Listen in audio

0:00 / 0:00

Dart is a modern, object-oriented, strongly typed programming language developed by Google. It is used to create mobile, web and desktop applications. One of the key features of Dart is the ability to compile to native code or JavaScript, allowing applications written in Dart to run on virtually any device. Flutter, also developed by Google, is a framework for developing user interfaces, which uses Dart as a programming language.

One of the most important aspects of modern application development is the ability to consume APIs. APIs, or Application Programming Interfaces, are sets of rules and protocols that allow different software to interact with each other. By consuming an API, an application can access functionality or data provided by other software, without needing to know its implementation details.

On Dart, consuming APIs is usually done through the http package, which provides a series of high-level functions for sending and receiving data over the HTTP protocol. This package can be added to the project through Dart's package manager, pub.

To consume an API in Dart, the first step is to import the http package. Then an HTTP request can be sent using the get, post, put, or delete function, depending on what needs to be done. These functions return a Future object, which represents an operation that will complete in the future. To deal with the API response, it is possible to use the then function of the Future object, which receives a callback function to be executed when the response is received.

Here's an example of how to consume an API in Dart:

Continue in our app.

You can listen to the audiobook with the screen off, receive a free certificate for this course, and also have access to 5,000 other free online courses.

Or continue reading below...
Download App

Download the app

import 'package:http/http.dart' as http; void main() async { var response = await http.get('https://api.example.com/items'); print('Response status: ${response.statusCode}'); print('Response body: ${response.body}'); }

In the example above, the get function of the http package is used to send a GET request to the API. The API response is then printed to the console. Note that the await keyword is used to wait for the API response before continuing program execution. This is necessary because the get function returns a Future object.

In addition to the response status and the response body, it is also possible to access the response headers through the headers property of the Response object. To send headers in the request, you can pass a string map to the get, post, put, or delete function.

In many cases, APIs return data in JSON format. To deserialize this data, Dart provides the dart:convert package, which includes the jsonDecode function. This function takes a JSON string and returns a Dart object that represents the data. To serialize data to JSON format, you can use the jsonEncode function from the same package.

Here's an example of how to deserialize JSON data into Dart:

import 'dart:convert'; import 'package:http/http.dart' as http; void main() async { var response = await http.get('https://api.example.com/items'); var items = jsonDecode(response.body); for (var item in items) { print('Item: ${item['name']}'); } }

In the example above, the jsonDecode function is used to convert the response body, which is a JSON string, into a list of maps. Each map represents an item, and each item's 'name' property is printed to the console.

In conclusion, Dart provides several powerful tools for consuming APIs, including the http package for sending and receiving HTTP requests, and the dart:convert package for serializing and deserializing JSON data. Combined with Flutter, Dart lets you build rich, responsive apps that can interact with any API.

Now answer the exercise about the content:

What is the primary function of the http package in Dart and how is it used to consume APIs?

You are right! Congratulations, now go to the next page

You missed! Try again.

The http package in Dart is primarily used to send and receive HTTP requests, enabling the consumption of APIs. It provides functions such as get, post, put, and delete to communicate with APIs and handle responses. This functionality is essential for interacting with external data and services in modern application development.

Next chapter

Dart Basics: Local Storage

Arrow Right Icon
Download the app to earn free Certification and listen to the courses in the background, even with the screen off.