collect method

Result<List<T>, E> collect()

Returns every successful value in order, or the first error.

Stops at the first Err: later elements of a lazy iterable are not read.

final ids = [const Ok<int, String>(1), const Ok<int, String>(2)].collect();
// Ok([1, 2])

Implementation

Result<List<T>, E> collect() {
  final values = <T>[];
  for (final result in this) {
    switch (result) {
      case Ok<T, E>(:final value):
        values.add(value);
      case Err<T, E>(:final error):
        return Err<List<T>, E>(error);
    }
  }
  return Ok<List<T>, E>(values);
}