Flutter › REST API Integration in Flutter

How do you properly structure a repository class specifically to encapsulate all of your app's own API calls related to one particular given resource, such as orders or products?

Beginner

Answer

A repository class properly encapsulates every single method genuinely related to fetching and manipulating one particular specific resource type, such as an OrderRepository containing methods like fetchOrders, createOrder, and cancelOrder, and this particular pattern keeps your actual networking specific code cleanly separated from the rest of your app's own UI and general business logic, meaning your widgets and other consuming code simply call a genuinely clear, well named repository method rather than needing to directly construct raw HTTP requests scattered throughout dozens of separate individual widgets.
class OrderRepository {
  Future<List<Order>> fetchOrders() async {
    final response = await dio.get('/orders');
    return (response.data as List).map((json) => Order.fromJson(json)).toList();
  }
}
Real-world example An e commerce app organizes all of its own order related API calls within a single dedicated OrderRepository class, letting the rest of the app simply call orderRepository.fetchOrders rather than needing to directly construct raw network requests scattered throughout several separate individual screens.

Common follow-ups: How does a repository class genuinely relate to the broader Clean Architecture pattern discussed earlier?;Should a repository class genuinely also be responsible for local caching in addition to just making network requests?