Junior — Middle
What methods of integration and interaction exist between REST API and Flutter applications?
sobes.tech AI
Answer from AI
To integrate Flutter applications with REST API, the http package or more advanced libraries like dio are usually used. The main method of interaction is sending HTTP requests (GET, POST, PUT, DELETE) to the REST API and handling responses.
Example of a simple request using the http package:
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 {
throw Exception('Failed to load data');
}
}
It is also common to:
- Parse JSON responses into data models.
- Handle errors and HTTP status codes.
- Implement authentication (e.g., via tokens in headers).
- Cache data and manage state using packages like
providerorbloc.
Thus, interaction is built on standard HTTP requests and response handling within a Flutter application.