Persistence
Store query data and pending mutations on the device, and they survive an app restart:
- A persisted query shows its last data right away, then refetches it in the background if it’s stale.
- A persisted mutation that was waiting for the network when the app closed runs again once the app calls
restore(mutations:)at the next start.
Connecting storage
Section titled “Connecting storage”Fuery reads and writes strings through a QueryStorage. Implement it against any key-value store. This one uses shared_preferences:
class PreferencesStorage implements QueryStorage { PreferencesStorage(this.preferences);
final SharedPreferencesWithCache preferences;
@override String? read(String key) => preferences.getString(key);
@override Future<void> write(String key, String value) => preferences.setString(key, value);
@override Future<void> delete(String key) => preferences.remove(key);
@override Map<String, String> readAll() => { for (final key in preferences.keys) if (key.startsWith(persistKeyPrefix)) key: preferences.getString(key)!, };}Every key Fuery writes starts with the persistKeyPrefix constant. Filter readAll by it, as above, so it returns Fuery’s entries and nothing else from the store.
Give the storage to the client:
Future<void> main() async { WidgetsFlutterBinding.ensureInitialized(); final preferences = await SharedPreferencesWithCache.create( cacheOptions: const SharedPreferencesWithCacheOptions(), ); Fuery.client = QueryClient(storage: PreferencesStorage(preferences)); runApp(const App());}SharedPreferencesWithCache reads synchronously, so Fuery restores a persisted query before its first frame. Storage methods may also return futures, for example for a database. See Restoring ahead of time.
Persisting a query
Section titled “Persisting a query”Add persist with functions that convert the data to JSON and back. Fuery stores only the queries that have persist:
final todosQuery = Query( queryKey: ['todos'], queryFn: (_) => api.getTodos(), persist: QueryPersist( toJson: (todos) => [for (final todo in todos) todo.toJson()], fromJson: (json) => [ for (final item in json! as List) Todo.fromJson(item), ], ),);- Converting: from
toJson, return a value thatjsonEncodeaccepts. If it can’t be encoded, Fuery doesn’t store the data and reports no error.fromJsonreceives whatjsonDecodeproduced, so cast it there, once.Todo.fromJsonabove takes anObject?. With a generatedTodo.fromJson(Map<String, dynamic> json), writeTodo.fromJson(item as Map<String, dynamic>). - Restoring: the first time the query is used, Fuery restores its stored data with the time it was fetched.
staleTimethen decides whether the query refetches, so fresh data isn’t fetched again. Restoring doesn’t need the network. - Storing: Fuery stores the data whenever it changes and no fetch is running, including changes made with
setData.client.setData(todosQuery, todos)stores even before anything uses the query, because the query it creates gets the definition’spersist. Fuery stores a streamed query once its stream is done. - Keys with enums: Fuery stores an enum in a key by its name, without its type. Obfuscated and minified builds can rename types in an app update, and the name alone still matches. Two persisted queries whose keys differ only in the type of a same-named enum therefore share one stored entry:
['todos', Filter.done]and['todos', Status.done]overwrite each other’s data. Add a string that tells them apart:['todos', 'filter', Filter.done].
Try it in the playground: restart the app, and the stored data is back at once, with the time it was fetched.
Persisting infinite queries
Section titled “Persisting infinite queries”Convert one page, and Fuery stores the list of pages:
final posts = InfiniteQuery( queryKey: ['posts'], queryFn: (context) => api.getPosts(page: context.pageParam), initialPageParam: 1, getNextPageParam: (data) => data.lastPage.hasMore ? data.lastPageParam + 1 : null, persist: InfiniteQueryPersist( pageToJson: (page) => page.toJson(), pageFromJson: (json) => PostPage.fromJson(json! as Map<String, Object?>), ),);Fuery stores every loaded page in one entry, and writes it again after each page loads. Set maxPages to keep a long feed from growing into a large entry.
Fuery stores page params as they are, so they must be JSON values, such as numbers, strings, or null. For other params, add paramToJson and paramFromJson. paramToJson receives each param as Object?, so cast it: paramToJson: (date) => (date! as DateTime).toIso8601String().
When stored data is discarded
Section titled “When stored data is discarded”Fuery discards stored data, and the query fetches as if nothing was stored, when the data:
- is older than the query’s
maxAge, or than the client’spersistMaxAge(default: 1 day) when the query sets nomaxAge. - has a
versionother than the query’s. Increaseversionwhen the JSON format changes. - can’t be decoded.
persist: QueryPersist( version: 2, maxAge: const Duration(hours: 6), toJson: (todos) => [for (final todo in todos) todo.toJson()], fromJson: (json) => [ for (final item in json! as List) Todo.fromJson(item), ],),restore() also deletes the stored queries that have expired, by the maxAge in effect when Fuery stored them. The data of a key the app no longer uses doesn’t stay in the storage.
Restoring ahead of time
Section titled “Restoring ahead of time”With a storage that reads asynchronously, a query shows its loading state until Fuery has read its data. To show the data on the first frame, read every stored entry before the app starts:
await Fuery.client.restore();runApp(const App());Deleting stored data
Section titled “Deleting stored data”| Call | Stored data |
|---|---|
removeQueries, resetQueries |
Deleted for the matching queries. A call that filters only by key also deletes the stored queries that aren’t loaded. |
clear() |
All deleted, queries and mutations. Call it when the user logs out. |
| Garbage collection | Kept. A query that leaves memory is restored the next time it’s used. |
Persisting mutations
Section titled “Persisting mutations”A mutation with persist stores the variables of each run from the moment it starts until it settles. A run that was paused offline, or still running, when the app closed is still stored at the next start. restore(mutations:) runs it again with the definition you pass, so the screen and main use the same definition:
Mutation<Comment, NewComment, void> addCommentMutation() { return Mutation( mutationKey: ['comments', 'add'], mutationFn: (NewComment comment) => api.addComment(comment), scope: const MutationScope('comments'), persist: MutationPersist( toJson: (comment) => {'postId': comment.postId, 'body': comment.body}, fromJson: (json) { final map = json! as Map<String, Object?>; return (postId: map['postId']! as int, body: map['body']! as String); }, ), onSuccess: (_, comment, __, client) { client.invalidateQueries(queryKey: ['comments', comment.postId]); }, );}
// In a screen:addCommentMutation().mutate( (postId: post.id, body: 'Nice post'), context.queryClient,);
// In main, before runApp:await Fuery.client.restore(mutations: [addCommentMutation()]);MutationPersist lists its parameters. A NoVariablesMutation persists with MutationPersist.noVariables: NoVariablesMutation(mutationKey: ['sync'], mutationFn: () => api.sync(), persist: MutationPersist.noVariables).
A request that reached the server before the app closed runs again after the restart. Persist only the mutations whose request is safe to repeat, or make the server treat a repeat as the same write.
Matching stored runs
Section titled “Matching stored runs”restorematches a stored run to its definition bymutationKey, so a persisted mutation needs one.mutationsis a list ofAnyMutation. EveryMutationis one, so definitions with different types go in one list.- Fuery stores the
mutationKeythe way it stores query keys, so the key can hold enums andDateTimes. - A key that can’t be stored, such as one holding an object without
toJson(), is reported once toonUncaughtError. The run goes on without being stored. restorereports two definitions whose keys differ only in enum types, and restores neither.
Restoring stored runs
Section titled “Restoring stored runs”restoreis the only way stored runs come back. It starts each one with its stored variables: right away while online, or when the network is back.- Runs that share a scope run one at a time, oldest first.
restorestarts each stored run once. It skips a run the client is already running or has paused, so calling it twice doesn’t repeat a request.- A restored run skips
onMutate, and its callbacks receivenullascontext. The optimistic update belongs to the run that made it. The restored run repeats only the request and the callbacks after it. - The MutationState widgets and
useMutationStateshow restored runs, found by the samemutationKey.MutationStateBuilder(mutation: addCommentMutation(), ...)lists the comments still on their way after a restart.
Deleting stored runs
Section titled “Deleting stored runs”- Fuery deletes a stored run once it succeeds or fails.
clear()deletes them all. - A stored run whose definition wasn’t passed to
restorestays, so a laterrestorecan run it. - Fuery deletes a stored run that another
versionof itsMutationPersiststored, or that it can’t read.
What Fuery guarantees
Section titled “What Fuery guarantees”- Fuery waits for a deletion in progress before it reads or writes, so a removed query never restores deleted data or writes over its own deletion.
- A restore still running when the query is reset or removed doesn’t bring the old data back.
- When a deletion overlaps a
restore()read,restore()reads again once the deletion is done, for at most 3 reads in all, so it never restores deleted data. If a deletion overlaps all 3 reads, that call restores nothing: queries restore when they’re first used, and stored mutations wait for the nextrestore(). - A query decides whether to fetch on mount after an asynchronous restore finishes, so
refetchOnMountandstaleTimetreat restored data like cached data. - Storage methods may be synchronous or asynchronous. Fuery ignores their errors: a query with a failing storage loads as if nothing was stored.
In the example app
Section titled “In the example app”- The preferences storage is a storage adapter.
- The feed queries persist the feed’s pages.
The example’s README maps each screen to what it shows.