toListOrEmpty method

List<T> toListOrEmpty()

Wraps a non-null value in a single-element list, or returns an empty list when null.

Distinct from MakeListExtensions.toListIfNotNull, which returns null (not an empty list) for a null receiver. Renamed from toListIfNotNull to avoid an ambiguous-extension clash with that established API and because "if not null" wrongly implied a nullable result.

Example:

5.toListOrEmpty();      // [5]
null.toListOrEmpty();   // []

Implementation

List<T> toListOrEmpty() {
  final T? self = this;
  if (self == null) return <T>[];
  return <T>[self];
}