Null-aware operator with Maps

匿名 (未验证) 提交于 2019-12-03 01:03:01

问题:

I had the same problem with lists, now it is Map.

What I would like to do

The following syntax is not Dart, as in it does not compile:

map?[key] ?? otherValue

If my map was not a Map but a List, it would look like Günter pointed out here:

list?.elementAt(index) ?? otherValue

What I am searching for

I understand that map?[key] is not valid syntax and therefore I am searching for something like elementAt, which works for lists, for maps.

map?.valueFor(key) ?? otherValue

valueOf

That does obviously not yet exist. The problem has solutions and valueOf might be a good one as well.

回答1:

This works:

(map ?? const {})[key] ?? otherValue;

Because the key will access an emtpy Map, which will always return null.



转载请标明出处:Null-aware operator with Maps
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!