On this page
article
Proxy
proxy is a decorator that intercepts function calls with before/after hooks and argument transformation.
Proxy
What it is
proxy is a decorator that intercepts function calls with before/after hooks and argument transformation.
When to use it
- Dependency injection and testing.
- A/B testing where you switch between implementations.
- Feature flags that change behavior dynamically.
API reference
| |
Parameters
| Parameter | Type | Description |
|---|---|---|
beforeCall | void Function(...)? | Invoked before the wrapped function. |
transformArg / transformArgs | T Function(T)? / (T1, T2) Function(T1, T2)? | Transforms arguments before passing them to the wrapped function. |
afterCall | R Function(R)? | Transforms the result after successful execution. |
onError | void Function(Object, StackTrace)? | Called when the wrapped function throws; the error is still rethrown. |
Examples
Basic example
| |
Real-world example
| |
Best practices
- Use
proxyfor cross-cutting concerns like logging, normalization, or enrichment. - Prefer
tapwhen you only need to observe results without transforming them.
Common pitfalls
onErroris called but the original exception is still rethrown.afterCallis not called when the function fails.