resolveAllValidated method

FutureOr<List<T>> resolveAllValidated(
  1. bool validate(
    1. T e
    ), {
  2. T? defaultValue,
})

Resolves all elements and validate them. If an element is not valid will use defaultValue.

Implementation

FutureOr<List<T>> resolveAllValidated(bool Function(T e) validate,
    {T? defaultValue}) {
  var self = this;

  if (_isNotFuture(T) && self is Iterable<T>) {
    return self.map((v) => validate(v) ? v : defaultValue as T).toList();
  }

  var all = allAsList;
  if (all.isEmpty) return <T>[];

  if (all.isAllResolved) {
    return all
        .cast<T>()
        .map((v) => validate(v) ? v : defaultValue as T)
        .toList();
  } else {
    return Future.wait(all.asFutures).then((l) {
      return l.map((v) => validate(v) ? v : defaultValue as T).toList();
    });
  }
}