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
Unhandled exception:
type '() => Null' is not a subtype of type '(() => int)?' of 'orElse'
#0 ListMixin.singleWhere (dart:collection/list.dart)
...
This is unexpected. For methods that modify the collection (x) there might be some awareness about problems with broken covariance (while also not great), but for something that doesn't modify the collection (like singleWhere) it's even less obvious.
This is as good as it gets with unsound covariant generics. You'd get a similar error just doing x.add(null);.
The problem, as you point out, is that you pass a List<int> to something expecting a List<int?>. That's allowed because List<int> is a subtype of List<int?>, but it's unsound for any member of List where the element type occurs contravariantly . That includes add and singleWhere.
The problem exists for any method where you need to produce a value of the element type in any other way than by reading it from the list, not just those trying to put that element into the list (although those are the most obvious ones). The singleWhere method claims to return a value which could be put into the list, and here it doesn't.
It's not special to null safety either, you would get a similar error for:
The following code has no static failures (as expected):
but it fails with:
This is unexpected. For methods that modify the collection (
x
) there might be some awareness about problems with broken covariance (while also not great), but for something that doesn't modify the collection (likesingleWhere
) it's even less obvious.Might be related to #33841.
Using:
The text was updated successfully, but these errors were encountered: