result<T> method

T result<T>([
  1. bool filter(
    1. T element
    )?
])

Implementation

T result<T>([bool filter(T element)?]) {
  final results =
      allResults.whereType<T>().where((_) => filter?.call(_) ?? true);
  if (results.isEmpty) {
    return illegalState(
        "No result found with type $T ${allResults.isEmpty ? '(the results were empty)' : '- but we found ${allResults.map((_) => _.resultType)}'}");
  } else if (results.length > 1) {
    return illegalState(
        "Found more than one result for $T, but found ${results.length}");
  } else {
    return results.first;
  }
}