remove static method
Create a mutation that optimistically removes/deletes a single value.
Automatically:
- Clears value from cache immediately (optimistic update)
- Rolls back on error
- Supports offline queueing
Example:
final logout = ZenMutation.remove(
queryKey: 'current_user',
mutationFn: () => api.logout(),
);
Implementation
static ZenMutation<void, void> remove({
required Object queryKey,
String? mutationKey,
required Future<void> Function() mutationFn,
void Function(Object? context)? onSuccess,
void Function(Object error)? onError,
}) {
return ZenMutation<void, void>(
mutationKey: mutationKey ?? '${queryKey}_remove',
mutationFn: (_) => mutationFn(),
onMutate: (_) async {
final oldValue = ZenQueryCache.instance.getCachedData(queryKey);
ZenQueryCache.instance.removeQuery(queryKey);
return oldValue;
},
onSuccess: (_, __, context) => onSuccess?.call(context),
onError: (err, _, context) {
if (context != null) {
ZenQueryCache.instance.setQueryData(
queryKey,
(_) => context,
);
}
onError?.call(err);
},
);
}