canAcceptPositionalArguments static method

bool canAcceptPositionalArguments(
  1. List args,
  2. Iterable<Parameter> parameters
)

Checks whether a list of positional arguments can be applied to a method's positional parameters.

Validates that the number of arguments satisfies the required and optional positional parameters.

Parameters

  • args: List of runtime argument values.
  • parameters: List of Parameter objects representing the method's parameters.

Returns

true if the number of arguments is within the allowed range of required and optional positional parameters; otherwise false.

Example

final parameters = method.getParameters();
final args = [1, 'hello'];

if (MethodUtils.canAcceptPositionalArguments(args, parameters)) {
  method.invokeWithArgs(instance, null, args);
}

Implementation

static bool canAcceptPositionalArguments(List<dynamic> args, Iterable<Parameter> parameters) {
  final positionalParams = parameters.where((p) => !p.isNamed());
  final requiredPositionalCount = positionalParams.where((p) => p.mustBeResolved()).length;

  return args.length >= requiredPositionalCount && args.length <= positionalParams.length;
}