withParams<T> static method

ZipRequest<T> withParams<T>(
  1. Function method,
  2. Map<String, dynamic> params, {
  3. String? tag,
})

Convenience factory for creating ZipRequest with string-keyed parameters.

This factory provides the simplest syntax for common cases where you need to pass parameters to a callback-based method. It automatically wraps the method call in a closure and converts string keys to Symbols.

Example:

ZipRequest.withParams<UserInfo>(
  getUserAsync,
  {'userId': '123', 'phone': '13800138000'},
  tag: 'user',
)

This is equivalent to the more verbose closure pattern:

ZipRequest<UserInfo>(
  request: ({success, failure, completed}) {
    getUserAsync(
      userId: '123',
      phone: '13800138000',
      success: success,
      failure: failure,
      completed: completed,
    );
  },
  tag: 'user',
)

Parameters:

  • method: The callback-based method to wrap
  • params: Map of parameter names to values (string keys)
  • tag: Optional identifier for named result access

Returns: A ZipRequest<T> that will invoke method with the specified parameters.

Note: The type parameter T must be explicitly specified as it cannot be inferred.

Implementation

static ZipRequest<T> withParams<T>(
  Function method,
  Map<String, dynamic> params, {
  String? tag,
}) {
  return ZipRequest<T>(
    request: ({Success<T>? success, Failure? failure, Completed? completed}) {
      final allParams = <Symbol, dynamic>{};

      // Add user parameters
      params.forEach((key, value) {
        allParams[Symbol(key)] = value;
      });

      // Add callbacks (these take precedence)
      if (success != null) allParams[#success] = success;
      if (failure != null) allParams[#failure] = failure;
      if (completed != null) allParams[#completed] = completed;

      Function.apply(method, [], allParams);
    },
    tag: tag,
  );
}