cover-img

What is API Call in flutter ?

30 March, 2023

1

1

0

In Flutter, an API call refers to the process of making a request to an application programming interface (API) in order to retrieve or send data.

APIs are used to allow applications to communicate with one another and share data. They can be used to retrieve data from a server, or to send data to a server for storage.

To make an API call in Flutter, you can use the http package. This package provides a simple way to make HTTP requests in Flutter, and to process the responses.

Here's an example of how you might use the http package to make an API call in Flutter:

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

void fetchData() async {
final response = await http.get('<https://example.com/api/endpoint>');

if (response.statusCode == 200) {
// If the call to the server was successful, parse the JSON
return response.body;
} else {
// If that call was not successful, throw an error.
throw Exception('Failed to load data');
}
}

In this example, the http.get() function is used to send a GET request to the specified API endpoint. If the request is successful (status code 200), the response body is returned. If the request is unsuccessful, an exception is thrown.

Supported API Verbs

In Flutter, you can use the http package to make various types of HTTP requests, depending on the desired action.

Here are the most common HTTP verbs that you can use with the http package in Flutter:

  • GET: Used to retrieve data from the server.
  • POST: Used to send data to the server for storage.
  • PUT: Used to update data on the server.
  • DELETE: Used to delete data from the server.

Here's an example of how you might use the http package to make a POST request in Flutter:

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

void sendData() async {
final response = await http.post(
'<https://example.com/api/endpoint>',
body: {'key': 'value'},
);

if (response.statusCode == 200) {
print('Data sent successfully');
} else {
throw Exception('Failed to send data');
}
}

In this example, the http.post() function is used to send a POST request to the specified API endpoint, along with the data to be stored. If the request is successful (status code 200), a message is printed. If the request is unsuccessful, an exception is thrown.

You can use similar syntax to make GETPUT, or DELETE requests, by using the http.get()http.put(), or http.delete() functions, respectively.

1

1

0

ShowwcaseHQ
Showwcase is where developers hang out and find new opportunities together as a community

More Articles

Showwcase is a professional tech network with over 0 users from over 150 countries. We assist tech professionals in showcasing their unique skills through dedicated profiles and connect them with top global companies for career opportunities.

© Copyright 2025. Showcase Creators Inc. All rights reserved.