Example cache is not keyed by user ID and can return one user's profile for another request
Source references: 2The example Repository stores only one `_cachedUser`. Once that cache is populated, `getUser(id)` returns it immediately without checking whether the cached user's ID matches the requested ID.
If the author or agent adopts this example in an application that queries multiple accounts, a shared Repository instance could show a later requester the previous user's name or any additional profile fields added to the model. It could also cause decisions to be made for the wrong account.
The example directly supports this risk, but only when one `UserRepository` instance is used to query different user IDs: once the cache is populated, the method returns the prior user without comparing the requested `id`. If copied into an app where the repository is shared across account or profile requests, it could display a previously queried user's data. This is instructional sample code, not proof that any app adopted it or exposed data. A user can ask the author to key the cache by ID or verify the cached user's ID before returning it.
final ApiClient _apiClient; User? _cachedUser; Future<User> getUser(String id) async { if (_cachedUser != null) return _cachedUser!; final apiModel = await _apiClient.fetchUser(id); _cachedUser = User(id: apiModel.id, name: apiModel.fullName); // Transform to Domain Model return _cachedUser!; }Show 1 other places
// 2. Repository (Single source of truth, returns Domain Model)class UserRepository { UserRepository({required ApiClient apiClient}) : _apiClient = apiClient; final ApiClient _apiClient; User? _cachedUser; Future<User> getUser(String id) async { if (_cachedUser != null) return _cachedUser!; final apiModel = await _apiClient.fetchUser(id); _cachedUser = User(id: apiModel.id, name: apiModel.fullName); // Transform to Domain Model return _cachedUser!; }