Repository Pattern
Abstract data sources behind a clean interface
The repository pattern abstracts where data comes from. Your app talks to a repository, not directly to APIs or databases.
A repository abstracts data sources behind a clean interface. The rest of the app does not know if data comes from an API, database, or cache.
// Abstract interface
abstract class UserRepository {
Future<User> getUser(int id);
Future<List<User>> getAll();
Future<void> save(User user);
Future<void> delete(int id);
}
// Concrete implementation
class ApiUserRepository
implements UserRepository {
final ApiClient _client;
@override
Future<User> getUser(int id) =>
_client.get('/users/$id');
}Architecture Layers
UI
WidgetsBlocBuilder
BLoC
EventsStates
Repository
InterfaceCaching
Data Sources
APIDatabaseCache
UI -> BLoC -> Repository -> Data Source