Command

The Command class allows calling a method or a function.

Implements: ICommand

Description

The Command class allows you to call a method or a function.

Constructors

Creates a new command object and assigns it’s parameters.

Command(String name, Schema? schema, IExecutable func)

  • name: String - command name.
  • schema: Schema? - schema to validate command arguments.
  • func: IExecutable - function to be executed by this command.

Instance methods

Execute

Executes the command. Before execution it validates args using the defined schema.

Raise ApplicationException when execution fails for whatever reason.
See Parameters

Future<dynamic> execute(String? correlationId, Parameters args)

  • correlationId: String?? - (optional) transaction id used to trace execution through the call chain.
  • args: Parameters - parameters (arguments) to pass to this command for execution.
  • returns: Future<dynamic> - execution result

getName

Gets the command name.

@override

String getName()

  • returns: String - name of this command.

validate

Validates the command args before execution using the defined schema.

@override

List<ValidationResult> validate(Parameters args)

  • args: Parameters - parameters (arguments) to validate using this command’s schema.
  • returns: List<ValidationResult> - array of ValidationResults or an empty array (if no schema is set).

Examples

var command =  Command('add', null, (correlationId, args) {
    var param1 = args.getAsFloat('param1');
    var param2 = args.getAsFloat('param2');
    var result = param1 + param2;
    return result;
});
result = await command.execute(
  '123',
  Parameters.fromTuples(
    ['param1', 2,
    'param2', 2]
  )).catch(err) {
    if (err!= null) print(err);
    else print('2 + 2 = ' + result);
  }
);

See also