Skip to content
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

Map does not have a null-aware-chainable "get" method #36541

Closed
srawlins opened this issue Apr 9, 2019 · 4 comments
Closed

Map does not have a null-aware-chainable "get" method #36541

srawlins opened this issue Apr 9, 2019 · 4 comments
Labels
area-core-library SDK core library issues (core, async, ...); use area-vm or area-web for platform specific libraries. library-collection

Comments

@srawlins
Copy link
Member

srawlins commented Apr 9, 2019

Take the following example:

void fn(List<Map> listOfMaps) {
  var x = listOfMaps?.elementAt(0)["key"];
}

I am accessing the first element of listOfMaps, guarding against null, with listOfMaps?.elementAt. However, if listOfMaps is null, then so is listOfMaps?.anything, so an exception will be raised when ultimately [] is called on null.

The solution to this for property access is to chain null-aware all the way down, i.e. foo?.bar.baz.quux should be foo?.bar?.baz?.quux to be safe. But this cannot be done for operators like [].

In the case of a List, you can replace list[i] with list?.elementAt(i), but there is no equivalent method for Map. I propose V get(K key) or V get(Object key).

@a-siva a-siva added area-core-library SDK core library issues (core, async, ...); use area-vm or area-web for platform specific libraries. library-collection labels Apr 10, 2019
@ThinkDigitalSoftware
Copy link

ThinkDigitalSoftware commented May 16, 2019

I agree. I've been having to work around this too by explicitly checking if the map is null first.
I like the optional get method, like Java has, allowing for myMapOrList?.get() or a myMapOrList?[keyOrIndex], which looks funny, but technically would fall in line with putting a ? after the object in question.

@ThinkDigitalSoftware
Copy link

In fact, I'd like to propose a few changes that touch on this point.
Since we have null operators to execute or assign if n object is null, why not include operators to run or execute if an object isn’t? say, myInt !? myInt++ or myObject !?= newValue;
also, if Dart allows for the null aware operators to be used in conjunction with operators, it would solve this problem and also allow for

myInt?++ //  does this get in the way of the syntax for the proposed non-nullable types?

@jamesderlin
Copy link
Contributor

Also see #28389.

BTW, I would be very much in favor of V get(K key) not only for null-aware-chainability but also to have a version of operator[] that type-checks the key.

@leafpetersen
Copy link
Member

We are adding a null aware subscript operator as part of the upcoming NNBD release. See additional discussion here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-core-library SDK core library issues (core, async, ...); use area-vm or area-web for platform specific libraries. library-collection
Projects
None yet
Development

No branches or pull requests

5 participants