A5, AEHS, Lahore, Pakistan
+92 306 77 57 681
APIs (Application Programming Interfaces) are essential for enabling communication between your Flutter app and external services. Whether you're fetching data from a server, sending data to a backend, or integrating third-party services, understanding how to work with APIs is crucial for any Flutter developer.
http package to your project.In your pubspec.yaml file, add the following dependency:
dependencies:
http: ^0.13.3
Import the http package in your Dart files where you plan to make API calls:
import 'package:http/http.dart' as http;ET Requests:
http.get method to fetch data from an API endpoint. Here’s an example: Future<void> fetchData() async {
final response = await http.get(Uri.parse('https://jsonplaceholder.typicode.com/posts'));
if (response.statusCode == 200) {
// Parse the JSON data
final List<dynamic> data = jsonDecode(response.body);
print(data);
} else {
throw Exception('Failed to load data');
}
}
http.post method to send data to an API endpoint. Here’s an example:Future<void> sendData() async {
final response = await http.post(
Uri.parse('https://jsonplaceholder.typicode.com/posts'),
headers: <String, String>{
'Content-Type': 'application/json; charset=UTF-8',
},
body: jsonEncode(<String, String>{
'title': 'Flutter Post Request',
'body': 'This is a sample post request',
'userId': '1',
}),
);
if (response.statusCode == 201) {
print('Data sent successfully');
} else {
throw Exception('Failed to send data');
}
}
Use the dart:convert library to decode JSON responses into Dart objects:
import 'dart:convert';
class Post {
final int id;
final String title;
final String body;
Post({required this.id, required this.title, required this.body});
factory Post.fromJson(Map<String, dynamic> json) {
return Post(
id: json['id'],
title: json['title'],
body: json['body'],
);
}
}
Future<List<Post>> fetchPosts() async {
final response = await http.get(Uri.parse('https://jsonplaceholder.typicode.com/posts'));
if (response.statusCode == 200) {
final List<dynamic> jsonData = jsonDecode(response.body);
return jsonData.map((json) => Post.fromJson(json)).toList();
} else {
throw Exception('Failed to load posts');
}
}
Implement proper error handling to manage various scenarios like network issues, server errors, and invalid responses:
Future<void> fetchDataWithErrorHandling() async {
try {
final response = await http.get(Uri.parse('https://jsonplaceholder.typicode.com/posts'));
if (response.statusCode == 200) {
final List<dynamic> data = jsonDecode(response.body);
print(data);
} else {
print('Server error: ${response.statusCode}');
}
} catch (e) {
print('Network error: $e');
}
}
Integrate the provider package to manage state efficiently while making API calls:
class PostProvider with ChangeNotifier {
List<Post> _posts = [];
List<Post> get posts => _posts;
Future<void> fetchPosts() async {
final response = await http.get(Uri.parse('https://jsonplaceholder.typicode.com/posts'));
if (response.statusCode == 200) {
final List<dynamic> jsonData = jsonDecode(response.body);
_posts = jsonData.map((json) => Post.fromJson(json)).toList();
notifyListeners();
} else {
throw Exception('Failed to load posts');
}
}
}
final response = await http.get(
Uri.parse('https://jsonplaceholder.typicode.com/posts'),
headers: {...},
).timeout(Duration(seconds: 10), onTimeout: () {
throw TimeoutException('The connection has timed out, please try again!');
});