toNullableValuesOf<T extends Object> function

Iterable<T?> toNullableValuesOf<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 may contain null values.

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

Implementation

Iterable<T?> toNullableValuesOf<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 if (list is Iterable<T>) {
    // value is an iterable with items of non-nullable type of the asked type
    return isExposed ? List<T?>.unmodifiable(list) : list.cast<T?>();
  } 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) => toNullableValueOf<T>(e));
    }
  }
}