This Flutter application demonstrates how to fetch and display data from a free API using Dart. The project uses the jsonplaceholder API to fetch user data and display it on the screen.
- Fetch data from an API
- Display data in a list view
- Error handling for failed API requests
An API (Application Programming Interface) is a set of rules and definitions that allows one software application to interact with another. It defines the methods and data structures that developers use to interact with the functionalities provided by a system. In this project, we use a RESTful API provided by jsonplaceholder to fetch user data.
import 'dart:convert';
import 'package:http/http.dart' as http;
import 'user_model.dart';
class UserApiServices {
String apiString = "https://jsonplaceholder.typicode.com/users";
Future<List<UserModel>> loadData() async {
final response = await http.get(Uri.parse(apiString));
if (response.statusCode == 200) {
final jsonData = jsonDecode(response.body);
List<UserModel> finalData =
jsonData.map<UserModel>((e) => UserModel.fromJson(e)).toList();
return finalData;
} else {
throw Exception('Failed to load API');
}
}
}This Flutter application demonstrates how to fetch and display images from the Pixabay API using Dart.
- Fetch images from the Pixabay API
- Display images in a grid view
- Error handling for failed API requests
import 'dart:convert';
import 'package:http/http.dart' as http;
class ApiService {
Future<String?> apiCalling(String query) async {
String api =
'https://pixabay.com/api/?key=44453202-45ef7c677649f9bab57181230&q=$query&image_type=photo';
Uri url = Uri.parse(api);
http.Response response = await http.get(url);
if (response.statusCode == 200) {
return response.body;
}
return null;
}
}
