ZipRequest<T> class
Wrapper for callback-based request methods with type information.
ZipRequest enables concurrent execution of callback-style network requests
by capturing the result type T and providing automatic callback injection.
It bridges the gap between callback-based APIs and concurrent execution patterns.
Why ZipRequest?
RxNet's callback-based execute() methods cannot be directly composed with
Future.wait(). ZipRequest solves this by:
- Wrapping callback methods with type information
- Automatically injecting internal callbacks to capture results
- Enabling parallel execution via zipRequest
- Preserving type safety throughout the process
Usage Patterns
Pattern 1: Closure Wrapper (Recommended)
Most flexible and type-safe. Explicitly pass all parameters:
ZipRequest<UserInfo>(
request: ({success, failure, completed}) {
getUserAsync(
userId: '123',
phone: '13800138000',
success: success,
failure: failure,
completed: completed,
);
},
tag: 'user',
)
Pattern 2: withParams Factory (Simplest)
Concise syntax for simple cases:
ZipRequest.withParams<UserInfo>(
getUserAsync,
{'userId': '123', 'phone': '13800138000'},
tag: 'user',
)
Pattern 3: Symbol Params (Advanced)
For dynamic invocation scenarios:
ZipRequest<UserInfo>(
request: getUserAsync,
params: {#userId: '123', #phone: '13800138000'},
tag: 'user',
)
Custom Callbacks
You can provide custom callbacks that will be invoked after internal callbacks:
ZipRequest<UserInfo>(
request: ({success, failure, completed}) {
getUserAsync(
userId: '123',
success: success,
failure: failure,
completed: completed,
);
},
tag: 'user',
success: (data, source) {
print('User loaded from $source');
},
failure: (error) {
print('Failed to load user: $error');
},
completed: () {
print('Request completed');
},
)
Type Safety
The generic type parameter T ensures compile-time type checking:
// Correct: Type matches method return type
ZipRequest<UserInfo>(request: getUserAsync, tag: 'user')
// Compile error: Type mismatch
ZipRequest<String>(request: getUserAsync, tag: 'user')
See also:
- zipRequest for executing concurrent requests
- ZipResults for accessing aggregated results
- withParams factory for simplified parameter passing
Constructors
-
ZipRequest({required ZipRequestFunction<
T> request, String? tag, Map<Symbol, dynamic> ? params}) -
Creates a ZipRequest with explicit type parameter
T.
Properties
- hashCode → int
-
The hash code for this object.
no setterinherited
-
params
→ Map<
Symbol, dynamic> ? -
Optional parameters to pass to the request method.
final
-
request
→ ZipRequestFunction<
T> -
The callback-based request method reference or closure.
final
- runtimeType → Type
-
A representation of the runtime type of the object.
no setterinherited
- tag → String?
-
Optional tag for named result access in ZipResults.
final
Methods
-
noSuchMethod(
Invocation invocation) → dynamic -
Invoked when a nonexistent method or property is accessed.
inherited
-
toString(
) → String -
A string representation of this object.
inherited
Operators
-
operator ==(
Object other) → bool -
The equality operator.
inherited
Static Methods
-
from<
T> (ZipRequestFunction< T> request, {String? tag, Map<Symbol, dynamic> ? params}) → ZipRequest<T> - Factory method for type inference from method signature.
-
withParams<
T> (Function method, Map< String, dynamic> params, {String? tag}) → ZipRequest<T> - Convenience factory for creating ZipRequest with string-keyed parameters.