invalidateQueries method

void invalidateQueries(
  1. QueryKey key, {
  2. bool exact = false,
})

Marks the query as stale. If the query is being used in a widget, it will be refetched, otherwise it will be refetched when it is used by a widget at a later point in time. Supports partial matching, take a look at the exact option.

Example:

final queryClient = useQueryClient();

// Invalidate every query with a key that starts with `post`
queryClient.invalidateQueries(['posts']);

// Both queries will be invalidated
final posts = useQuery(['posts'], getPosts);
final post = useQuery(['posts', 1], getPosts);

// Use `exact: true` to exactly match the query
queryClient.invalidateQueries(['posts'], exact: true);

// Only this will invalidate
final posts = useQuery(['posts'], getPosts);

Implementation

void invalidateQueries(QueryKey key, {bool exact = false}) {
  queryCache.queries.forEach((queryKey, query) {
    if (exact) {
      if (queryKey == key.lock) {
        query.dispatch(DispatchAction.invalidate, null);
      }
    } else {
      final isPartialMatch = queryKey.length >= key.length &&
          queryKey.sublist(0, key.length) == key.lock;

      if (isPartialMatch) {
        query.dispatch(DispatchAction.invalidate, null);
      }
    }
  });
}