ZenScopeQueryExtension extension
Extension methods for ZenScope to simplify scoped query registration.
These methods combine query creation and scope registration in a single call, reducing boilerplate and preventing common mistakes like forgetting to register the query or passing the wrong scope.
Example usage in a module:
class ProductModule extends ZenModule {
final String productId;
ProductModule(this.productId);
@override
void register(ZenScope scope) {
// Register scoped query - auto-disposes when scope disposes
scope.putQuery<Product>(
queryKey: 'product:$productId',
fetcher: (_) => api.getProduct(productId), // Updated signature
);
// Or use the cached variant with sensible defaults
scope.putCachedQuery<List<Review>>(
queryKey: 'reviews:$productId',
fetcher: (_) => api.getReviews(productId), // Updated signature
staleTime: Duration(minutes: 10), // Optional: override default
);
}
}
- on
Methods
-
putCachedQuery<
T> ({required Object queryKey, required ZenQueryFetcher< T> fetcher, Duration staleTime = const Duration(minutes: 5), T? initialData}) → ZenQuery<T> -
Available on ZenScope, provided by the ZenScopeQueryExtension extension
Register a scoped query with common caching defaults. -
putQuery<
T> ({required Object queryKey, required ZenQueryFetcher< T> fetcher, ZenQueryConfig? config, T? initialData}) → ZenQuery<T> -
Available on ZenScope, provided by the ZenScopeQueryExtension extension
Register a scoped query with automatic lifecycle management.