3.15. Dart Basics: Consuming APIs

Página 33

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:

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.

Next page of the Free Ebook:

343.16. Dart Basics: Local Storage

Earn your Certificate for this Course for Free! by downloading the Cursa app and reading the ebook there. Available on Google Play or App Store!

Get it on Google Play Get it on App Store

+ 6.5 million
students

Free and Valid
Certificate with QR Code

48 thousand free
exercises

4.8/5 rating in
app stores

Free courses in
video, audio and text