one method
Returns true
if there is exactly one element of this
which satisfies
test
.
Example:
[1, 2, 3].one((e) => e == 2); // 1 element satisfies. Returns true.
[1, 2, 3].one((e) => e > 4); // No element satisfies. Returns false.
[1, 2, 3].one((e) => e > 1); // >1 element satisfies. Returns false.
Implementation
bool one(bool Function(E element) test) {
bool foundOne = false;
for (final e in this) {
if (test(e)) {
if (foundOne) return false;
foundOne = true;
}
}
return foundOne;
}