useQuery<T extends dynamic> function
Implementation
QueryResponse<T> useQuery<T extends dynamic>(
BuildContext context,
dynamic key,
Function func,
) {
String cacheKey = _transformKey(key);
_QueryNotifier provider = Provider.of<_QueryNotifier>(context, listen: true);
if (!provider.cache.containsKey(cacheKey)) {
provider.executeAndStoreQuery(func, cacheKey);
return QueryResponse<T>(data: null, loading: true, error: null);
} else {
QueryResponse cachedResponse = provider.cache[cacheKey];
bool shouldRefetch = provider.refetchedQueries.containsKey(cacheKey) &&
provider.refetchedQueries[cacheKey];
if (shouldRefetch) {
provider.executeAndStoreQuery(func, cacheKey);
return QueryResponse<T>(
data: cachedResponse.data, loading: true, error: null);
}
if (cachedResponse.error != null) {
return QueryResponse<T>(
data: null, loading: false, error: cachedResponse.error);
}
return QueryResponse<T>(
data: cachedResponse.data, loading: false, error: null);
}
}