toNonNullSet<T> function

Set<T> toNonNullSet<T>(
  1. Set? set, {
  2. bool forceTypeCast = true,
})

Converts set to a Set<T> ignoring any null element.

  • forceTypeCast: if true, will try to cast elements as T. if false, will ignore any element that can't be cast.

Implementation

Set<T> toNonNullSet<T>(Set? set, {bool forceTypeCast = true}) {
  if (set == null || set.isEmpty) {
    return <T>{};
  }

  Set<T> set2;
  if (forceTypeCast) {
    set2 = set.where((e) => e != null).map((e) => e! as T).toSet();
  } else {
    set2 = set.whereType<T>().toSet();
  }
  return set2;
}