toValuesOf<T extends Object> function

Iterable<T> toValuesOf<T extends Object>(
  1. Iterable<Object?> list, {
  2. bool isExposed = false,
})

Converts list to a value iterable of T or throws if cannot convert.

The returned iterable cannot contain null values.

Supported types for T: String, num, int, BigInt, double, bool, DateTime, Identifier, Object.

Implementation

Iterable<T> toValuesOf<T extends Object>(
  Iterable<Object?> list, {
  bool isExposed = false,
}) {
  if (list is Iterable<T>) {
    // value is an iterable with items of the asked type
    return isExposed ? List<T>.unmodifiable(list) : list;
  } else {
    // value is an iterable with items of any type
    if (T == Object) {
      // requesting Iterable<Object>
      return isExposed ? List<T>.unmodifiable(list) : list.cast<T>();
    } else {
      // requesting more specific types than Object, map items to T
      return list.map<T>((dynamic e) => toValueOf<T>(e));
    }
  }
}