firstNonNull method
T?
firstNonNull()
Returns the first non-null element or null if none exists.
final numbers = null, null, 5, 10, null
;
final emptyList = null;
print(numbers.firstNonNull()); // Output: 5 print(emptyList.firstNonNull()); // Output: null
Implementation
T? firstNonNull() {
final nonNullIterable = this?.whereType<T>();
return nonNullIterable != null && nonNullIterable.isNotEmpty
? nonNullIterable.first
: null;
}