castSet method

Set? castSet(
  1. Set set,
  2. Type type, {
  3. bool nullable = false,
})

Cast set to reflectedType if type == reflectedType or return null.

  • If nullable is true casts to a Set of nullable values.

Implementation

Set? castSet(Set set, Type type, {bool nullable = false}) {
  if (type == reflectedType) {
    if (nullable) {
      if (set is Set<O?>) {
        return set;
      } else {
        return Set<O?>.from(set);
      }
    } else {
      if (set is Set<O>) {
        return set;
      } else {
        return Set<O>.from(set);
      }
    }
  } else if (type == dynamic) {
    return Set<dynamic>.from(set);
  } else if (type == Object) {
    return nullable ? Set<Object?>.from(set) : Set<Object>.from(set);
  }

  var typeInfo = TypeInfo.fromType(type);

  Set? callCast<E>() => set.cast<E>();
  Set? callCastNullable<E>() => set.cast<E?>();

  var l = nullable
      ? typeInfo.callCasted(callCastNullable)
      : typeInfo.callCasted(callCast);

  return l;
}