Sub topic 5
Repositories and Data Sources in a Feature
Assume you know Flutter widgets, Dart imports, one state-management approach, and the basic repository pattern. This chapter focuses on where repositories and data sources belong in a feature-first project.
Separate the domain from storage
A data source talks to one storage system. Examples include a REST API, SQLite, a local file, or an in-memory list.
A repository gives the rest of the feature a stable interface. It decides which data source to use and converts storage data into domain objects.
For a cart feature, the folders might look like this:
lib/features/cart/
data/
cart_api.dart
cart_item_dto.dart
cart_repository_impl.dart
domain/
cart_item.dart
cart_repository.dart
presentation/
cart_page.dart
cart_controller.dartThe domain layer should not import HTTP clients, database packages, or Flutter widgets. The data layer can depend on the domain layer, but the reverse should not happen.
Define the repository in the domain layer
The repository interface describes what the feature needs, not how storage works.
class CartItem {
const CartItem({required this.id, required this.quantity});
final String id;
final int quantity;
}
abstract interface class CartRepository {
Future<List<CartItem>> loadItems();
Future<void> addItem(CartItem item);
}
class CartItemDto {
const CartItemDto({required this.id, required this.quantity});
final String id;
final int quantity;
CartItem toDomain() {
return CartItem(id: id, quantity: quantity);
}
}
abstract interface class CartDataSource {
Future<List<CartItemDto>> readItems();
Future<void> writeItem(CartItemDto item);
}
class InMemoryCartDataSource implements CartDataSource {
final List<CartItemDto> _items = [];
@override
Future<List<CartItemDto>> readItems() async {
return List.unmodifiable(_items);
}
@override
Future<void> writeItem(CartItemDto item) async {
_items.add(item);
}
}
class CartRepositoryImpl implements CartRepository {
CartRepositoryImpl(this._source);
final CartDataSource _source;
@override
Future<List<CartItem>> loadItems() async {
final records = await _source.readItems();
return records.map((record) => record.toDomain()).toList();
}
@override
Future<void> addItem(CartItem item) {
final record = CartItemDto(id: item.id, quantity: item.quantity);
return _source.writeItem(record);
}
}
Future<void> main() async {
final repository = CartRepositoryImpl(InMemoryCartDataSource());
await repository.addItem(const CartItem(id: 'book-1', quantity: 2));
final items = await repository.loadItems();
print('${items.first.id}: ${items.first.quantity}');
}CartItem is a domain object. CartItemDto is a data-transfer object, or DTO, shaped for storage. The repository performs the conversion. If an API later changes its JSON fields, presentation code does not need to change.
Choose the right data source
A feature can have several data sources:
- A remote source loads current data from a server.
- A local source stores cached or offline data.
- A memory source helps with tests and temporary state.
The repository can coordinate them. For example, it can read cached products first, refresh from the API, save the response locally, and return domain objects. That policy belongs in the repository, not in a widget.
Test the boundary
Test the repository with a fake data source. Verify mapping, caching rules, and error behavior without making network requests. Test the data source separately with an API or database test.
A common mistake is injecting a concrete API client directly into a controller. That couples presentation to storage and makes tests slow or fragile. Inject CartRepository instead.
Exercise
Where should JSON decoding happen, and why?
Show answer
It should happen in the data layer, usually in a DTO or data source. This keeps storage details out of domain and presentation code.