-
Notifications
You must be signed in to change notification settings - Fork 205
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Null-aware collection-for #3730
Comments
It does not exist. I'm not sure it will. One issue is that you should rarely have a nullable collection, it's better to use an empty collection, and not have two ways to represent "no elements". If we get Null-aware elements (#323), then you should be able to write: for (final i in [?myInts]) { ... } and with a little luck and prodding, the compiler should be able to optimize that into: if (myInts case final myInts?) for (final i in myInts) { ... } (if you don't want to write that out yourself). |
while largely I agree, one situation I have run into this is optional function parameters. |
@anqit https://dart.dev/language/pattern-types#null-check It matches if |
That wouldn't do what you want because it would only unwrap the outer list, so if for (final i in [...?myInts]) { ... } That's a little silly, but does work and do the right thing. Figuring out what sort of null-aware operations are worth adding to Dart and which aren't is hard. In theory, we could support null-aware operations for every possible operation that has a reasonable "do nothing" semantics to fall back on when the value is But in most cases, I think it's easy enough to use |
I think we should guarantee that a spread of a list literal, (Or introduce a new element syntax for inlining a number of elements in the current collection, fx |
I like this idea. Anytime I'm writing an if statement in a List (like I do a lot in Flutter) I always get worried about using the spread operator like so: return Column(
children: [
if (condition) ...[
FooWidget(),
const SizedBox(height: 10),
],
],
); Don't mean to hijack this issue, just saw Irhn's comment and wanted to reply to it |
Don't worry about it (unless your profiler tells you otherwise). If you were to not use the spread operator, the code you would write instead (like calling |
Currently, for collection-for's on nullable collections, we need a null check:
It would be nice if we could have a null-aware version of this syntax, something along the lines of
or
which would skip iteration if the iterable were
null
.Sorry if this already exists and I'm just (null-)unaware of it :)
The text was updated successfully, but these errors were encountered: