-
Notifications
You must be signed in to change notification settings - Fork 87
Add Iterable.elementAtOrNull extension #217
Add Iterable.elementAtOrNull extension #217
Conversation
Thanks for your pull request. It looks like this may be your first contribution to a Google open source project (if not, look below for help). Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA). 📝 Please visit https://cla.developers.google.com/ to sign. Once you've signed (or fixed any issues), please reply here with What to do if you already signed the CLAIndividual signers
Corporate signers
ℹ️ Googlers: Go here for more info. |
At a minimum, we should have a test here. Not sure about the overall usefulness of this extension, though. CC @natebosch |
I don't think the method is sufficiently useful.
has the same null-on-out-of-bounds behaviour as
but the latter is shorter. |
I think |
|
I see.I mistakenly think that only the constant list will use ListMapView.I find that ListMapView is always used. |
+1 on this being too specialized to put into a core package. If we do decide to land this functionality it should be called https://api.dart.dev/stable/2.14.2/dart-core/Iterable/elementAt.html |
|
The An So, I'm still hesitant to whether it's really needed, but if so, an extension on |
@lrhn I just add |
(This fells like something I'd want to solve generally, instead of introducing extension methods for each possible way to access a list. Maybe we could create a Until then, this works. |
Do you mean you'd like to go forward with this change? Should I start getting it ready to land? |
Yes, it's good. Get ready to land! Not waiting for view types. |
I've applied the feedback so far. This is ready for another round of review. |
/// | ||
/// The [index] must not be negative. | ||
E? elementAtOrNull(int index) => (index < length) ? this[index] : null; | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Curious that there is no firstOrNull
/lastOrNull
defined on List
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM!
@@ -1,5 +1,5 @@ | |||
name: collection | |||
version: 1.16.1-dev |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's OK to go directly to 1.17.0 and publish.
Add extensions on List and Iterable to read an element without risk of exceptions.
Add a extension function for List
getOrNull(int index)
returns a element at the given [index] of this list, if [index] out of bounds, return
null
Example: