Skip to main content

Cookbook

The guides explain one concept each. A recipe starts from a task instead — "refresh a token once for every request that hit it", "load the next page before the user reaches the end" — and puts together the pieces it needs, as complete code in the shape of an app: which file each part lives in, what each step does, the traps, and the variations.

The recipes below build one small app between them — a product catalogue with a list, a search, a detail screen, an edit form and an endless feed — so the code on one page is the code the next page uses.

Data and networking​

RecipeWhat it answers
Wiring dio or package:httpOne API client: cancellation handed to the transport, timeouts, readable errors
Auth and token refreshRefresh a token once, retry only what can succeed, a cache per signed-in user
List to detail, seededOpen a detail screen with the data the list already has
Lifecycle and connectivity wiringconnectivity_plus, a reachability probe, calmer focus refetches, an offline switch

UI patterns​

RecipeWhat it answers
Pull to refreshA RefreshIndicator that lasts as long as the refetch and keeps the list on failure
Search as you typeDebounced, cancelled when superseded, the last results kept while the next load
Forms and server validationA mutation-driven form with the server's field errors next to the fields
A global error snackbarOne toast for failed refreshes and saves, and a per-query way out
An infinite list viewLoad the next page near the end, once per page, with a footer for the rest

Testing​

RecipeWhat it answers
Testing a screenA fake API with latency, a harness with the teardown, and tests that step fake time

Architecture and integration​

How query_kit fits into the rest of an app: state management, injection, routing, persistence, realtime data, devices, accounts and models.

RecipeWhat it answers
Next to Riverpod, Bloc or ProviderServer state in the cache, app state in your store, connected through QueryController
Offline first, and surviving a restartSave chosen queries and unsent writes to disk, and restore them before the first frame
Where the client livesOne client, provided at the root, reached with of, maybeOf and read, and shared with get_it
Routing with go_routerKeys from path parameters, a prefetch on tap, a refetch on return, reads in dialogs
Realtime updates over a WebSocketServer events write to the cache or invalidate it, with a resync after a reconnect
Poll until a device confirmsAn accepted write, a poll that starts and stops itself, and a state for giving up
Disconnecting a deviceNo request to a device the user disconnected, and no old reading left in the cache
Sign out and multiple accountsA fresh cache per user, cleared after the screens, and accounts kept apart by prefix
Normalised data or one key per entityA list plus per-item keys, or a map by id, with unchanged items kept
Models with freezed and JSONValue equality, and StructurallyShareable for a class that wraps a list

Every sample on this site is compiled and checked against its source, so a recipe you copy here compiles against the current release. The few that need dio, package:http or connectivity_plus — packages this library does not depend on — say so above the code.