You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
My proposal: Deprecate and delete singleWhereOrNull, and introduce more clear methods
Reasoning:
Many use the singleWhere method as replacement to firstWhere when the list should not contain more than one matching value, in cases when the logic would not work if code somewhere later changes in way that there are multiple matching values, so throwing error would make sense (developer need to go and handle the case).
But when also no matching value is acceptable, developer can mistakenly use singleWhereOrNull and have different behaviour than intended. These cases are not so commonly unit tested, as these cases are preventing mistakes in the future.
The text was updated successfully, but these errors were encountered:
I can see how the singleWhereOrNull behavior might not be what someone wants, if they're really asking for a zeroOrOneWhere: If there is none, give me null, if there is one, give me it, if there are more, something is seriously wrong, so throw.
I do think that elements.singleWhereOrNull(test) should behave like elements.where(test).singleOrNull, so maybe we just need to add another method to cover the zeroOrOneWhere/optionalSingleWhere case.
https://github.com/dart-lang/collection/blob/v1.17.2/lib/src/iterable_extensions.dart#L296-L304
My proposal: Deprecate and delete
singleWhereOrNull
, and introduce more clear methodsReasoning:
Many use the
singleWhere
method as replacement tofirstWhere
when the list should not contain more than one matching value, in cases when the logic would not work if code somewhere later changes in way that there are multiple matching values, so throwing error would make sense (developer need to go and handle the case).But when also no matching value is acceptable, developer can mistakenly use
singleWhereOrNull
and have different behaviour than intended. These cases are not so commonly unit tested, as these cases are preventing mistakes in the future.The text was updated successfully, but these errors were encountered: