Junior — Middle
How did you make calls and receive responses from external APIs in your projects?
sobes.tech AI
Answer from AI
In Flutter, external APIs are usually called using the http package or more advanced libraries, such as dio.
Main approach:
- Import the
httppackage. - Make an asynchronous request (GET, POST, etc.) to the desired URL.
- Handle the response, for example, parse JSON.
Example using http:
import 'package:http/http.dart' as http;
import 'dart:convert';
Future<void> fetchData() async {
final response = await http.get(Uri.parse('https://api.example.com/data'));
if (response.statusCode == 200) {
final data = jsonDecode(response.body);
print(data);
} else {
print('Error: ${response.statusCode}');
}
}
This approach makes it easy to integrate external services, retrieve data, and use it in your application.