warmCache static method
Pre-populates the cache by firing all requests concurrently.
Call this after configureNetworkService — typically at the end of your app startup sequence — so data is immediately available on first render without any loading state.
Each request is independent: a failure on one does not affect the others. Errors are silently swallowed (they are logged in debug mode).
await NetworkService.warmCache([
WarmCacheRequest(endPoint: '/profile', method: ApiMethods.get),
WarmCacheRequest(
endPoint: '/products',
method: ApiMethods.get,
queryParams: {'limit': 20},
cacheDuration: Duration(hours: 6),
),
]);
Implementation
static Future<void> warmCache(List<WarmCacheRequest> requests) async {
if (requests.isEmpty) return;
await Future.wait(
requests.map((r) async {
try {
await _apiRequest.getResponse(
endPoint: r.endPoint,
apiMethods: r.method,
queryParams: r.queryParams,
body: r.body,
cacheConfig: CacheConfig(
isToCache: true,
isToLoadDataFromCache: false,
cacheDuration: r.cacheDuration,
),
);
} catch (e) {
if (kDebugMode) {
debugPrint('[NetworkService] warmCache failed for ${r.endPoint}: $e');
}
}
}),
);
}