Request for findFirst/findLast/findSingle extension methods in SDK #43221
Labels
area-core-library
SDK core library issues (core, async, ...); use area-vm or area-web for platform specific libraries.
library-core
NNBD
Issues related to NNBD Release
Several users have run into trouble migrating code using
firstWhere
to null safety, specifically when using a pattern like this:The "obvious" migration of this doesn't work:
because the closure
() => null
is inferred to have the return typeint
(and hence returningnull
is not allowed).The migration tool currently produces:
which doesn't work because any caller passing an iterable of non-nullable ints to
foo
will now fail.There's been some discussion of this issue at dart-lang/language#836 but that bug seems to be focused on a more general problem. There's also some discussion at #42947, but it's confusing because it looks like that bug was moved to dart-lang/language#836. I'm currently running into this trying to migrate protobufs, and it's been independently hit by Michal Terepeta and @srawlins, so I'm opening this bug to try to get the issue more attention.
The workaround I'm currently using is to write my own extension:
It would be nice if this extension could be provided by the core SDK rather than requiring users to re-invent it. Particularly because then the migration tool could be programmed to automatically make use of it. If we had such an extension in the SDK, it would make sense to also include
findLast
andfindSingle
(parallelinglastWhere
andsingleWhere
).The text was updated successfully, but these errors were encountered: