execute method

  1. @override
void execute([
  1. TParam? param
])
override

Calls the wrapped handler function with an optional input parameter

Implementation

@override
void execute([TParam? param]) {
  if (!_canExecute.value) {
    return;
  }
  _thrownExceptions.value = null;
  try {
    TResult result;
    if (_noParamValue) {
      assert(_funcNoParam != null);
      result = _funcNoParam!();
    } else {
      assert(_func != null);
      assert(param != null || null is TParam,
          'You passed a null value to the command ${_debugName ?? ''} that has a non-nullable type as TParam');
      result = _func!(param as TParam);
    }
    if (!_noReturnValue) {
      _commandResult.value =
          CommandResult<TParam, TResult>(param, result, null, false);
      value = result;
    } else {
      notifyListeners();
    }
    _futureCompleter?.complete(result);
  } catch (error) {
    if (error is AssertionError) rethrow;

    _commandResult.value = CommandResult<TParam, TResult>(param,
        _includeLastResultInCommandResults ? value : null, error, false);
    if (_commandResult.listenerCount < 3 && !_thrownExceptions.hasListeners) {
      /// we have no external listeners on [results] or [thrownExceptions]
      Command.globalExceptionHandler
          ?.call(_debugName, CommandError(param, error));
      _futureCompleter?.completeError(error);
      if (!_catchAlways) {
        rethrow;
      }
    }
  } finally {
    Command.loggingHandler?.call(_debugName, _commandResult.value);
  }
}