Command<TParam, TResult> constructor
Command<TParam, TResult> ({
- required TResult initialValue,
- required ValueListenable<
bool> ? restriction, - required ExecuteInsteadHandler<
TParam> ? ifRestrictedExecuteInstead, - required bool includeLastResultInCommandResults,
- required bool noReturnValue,
- required bool notifyOnlyWhenValueChanges,
- ErrorFilter? errorFilter,
- required String? debugName,
- required bool noParamValue,
Implementation
Command({
required TResult initialValue,
required ValueListenable<bool>? restriction,
required ExecuteInsteadHandler<TParam>? ifRestrictedExecuteInstead,
required bool includeLastResultInCommandResults,
required bool noReturnValue,
required bool notifyOnlyWhenValueChanges,
ErrorFilter? errorFilter,
required String? debugName,
required bool noParamValue,
}) : _restriction = restriction,
_ifRestrictedExecuteInstead = ifRestrictedExecuteInstead,
_noReturnValue = noReturnValue,
_noParamValue = noParamValue,
_includeLastResultInCommandResults = includeLastResultInCommandResults,
_errorFilter = errorFilter ?? errorFilterDefault,
_debugName = debugName,
super(
initialValue,
mode: notifyOnlyWhenValueChanges
? CustomNotifierMode.normal
: CustomNotifierMode.always,
) {
_commandResult = CustomValueNotifier<CommandResult<TParam?, TResult>>(
CommandResult.data(null, initialValue),
);
/// forward error states to the `errors` Listenable
_commandResult.where((x) => x.hasError).listen((x, _) {
_errors.value = CommandError<TParam>(
x.paramData,
x.error,
command: this,
commandName: this._debugName,
);
_errors.notifyListeners();
});
/// forward busy states to the `isExecuting` Listenable
_commandResult.listen((x, _) => _isExecuting.value = x.isExecuting);
/// Merge the external execution restricting with the internal
/// isExecuting which also blocks execution if true
_canExecute = (_restriction == null)
? _isExecuting.map((val) => !val) as ValueNotifier<bool>
: _restriction!.combineLatest<bool, bool>(
_isExecuting,
(restriction, isExecuting) => !restriction && !isExecuting,
) as ValueNotifier<bool>;
}