singleOrNull method
Returns the single element matching the given predicate
, or null
if element was not found or more than one element was found.
Implementation
T? singleOrNull([bool Function(T)? predicate]) {
if (predicate == null) {
if (size == 1) {
return get(0);
} else {
return null;
}
} else {
T? single;
var found = false;
for (final element in iter) {
if (predicate(element)) {
if (found) return null;
single = element;
found = true;
}
}
if (!found) return null;
return single;
}
}