useQueryClient function

QueryClient useQueryClient([
  1. QueryClient? client
])

A hook that retrieves the QueryClient from the widget tree.

If client is provided, it is returned directly. Otherwise, this hook reads the QueryClient from the nearest QueryClientProvider ancestor in the widget tree.

Throws a FlutterError if no QueryClientProvider is found and client is not provided.

Example:

Widget build(BuildContext context) {
  final queryClient = useQueryClient();
  // Use queryClient...
}

Implementation

QueryClient useQueryClient([QueryClient? client]) {
  final inheritedClient = use(const _UseQueryClientHook());

  if (client != null) {
    return client;
  }

  if (inheritedClient == null) {
    throw FlutterError(
      'QueryClientProvider not found in widget tree.\n'
      'Make sure to wrap your widget tree with QueryClientProvider:\n'
      'QueryClientProvider(\n'
      '  create: (context) => QueryClient(),\n'
      '  child: MyApp(),\n'
      ')',
    );
  }

  return inheritedClient;
}