具有地图的空感知运算符 [英] Null-aware operator with Maps

查看:56
本文介绍了具有地图的空感知运算符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有与列表相同的问题,现在是 地图

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

以下语法不是 Dart ,如一样不会编译

map?[key] ?? otherValue

如果我的地图不是 地图 ,但有一个 List ,它看起来像Günter在这里指出

list?.elementAt(index) ?? otherValue



我要搜索的内容



我了解 map?[key] 是无效的语法,因此我正在搜索类似 elementAt ,适用于列表,地图。

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



显然不是还存在该问题已解决,并且 valueOf 可能是

推荐答案

这可行:

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

因为将回退以访问地图 ,它将始终返回 null

这篇关于具有地图的空感知运算符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆