validateAsync method

  1. @override
Future<bool> validateAsync({
  1. bool debounce = false,
})
override

Runs synchronous validators first, and if they pass, evaluates all asyncValidators.

If debounce is true, delays execution by asyncDebounce. Stale in-flight runs are discarded.

await usernameField.validateAsync();

Implementation

@override
Future<bool> validateAsync({bool debounce = false}) async {
  _debounceTimer?.cancel();
  _debounceTimer = null;
  final seq = ++_asyncValidationSeq;

  if (debounce && asyncDebounce > Duration.zero && asyncValidators.isNotEmpty) {
    _isValidating.value = true;
    final completer = Completer<bool>();
    _debounceTimer = Timer(asyncDebounce, () async {
      if (seq != _asyncValidationSeq) {
        completer.complete(false);
        return;
      }
      final result = await _executeValidation(seq);
      if (!completer.isCompleted) completer.complete(result);
    });
    return completer.future;
  }

  return _executeValidation(seq);
}