dart - 什么是 ?? Dart 中的双问号?

标签 dart operators

下面的代码行有两个问号:

final myStringList = prefs.getStringList('my_string_list_key') ?? [];

什么意思?

最佳答案

?? 双问号运算符表示“if null”。以下面的表达式为例。

String a = b ?? 'hello';

这意味着 a 等于 b,但如果 b 为 null,则 a 等于 'hello '

另一个相关的运算符是??=。例如:

b ??= 'hello';

这意味着如果b为空,则将其设置为等于hello。否则,请勿更改。

引用

条款

Dart 1.12 release news以下统称为空感知运算符:

  • ?? -- if null 运算符
  • ??= -- null 感知赋值
  • x?.p -- null 感知访问
  • x?.m() -- null 感知方法调用

关于dart - 什么是 ?? Dart 中的双问号?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54031804/

相关文章:

linux - Dart pub 安装 x86_64 SocketIOException

python 3 : apply an operator over an iterable

斯卡拉 : Is there a better way to evaluate an expression tree?

web-services - POST API不起作用,但在 flutter 打中的 postman 上有效

xml - 如何从 XmlEvent 访问 XML 元素名称

asynchronous - Dart-为什么我必须等待这个.fold电话

php - ** 是新的幂运算符,而不是 php 中的 power()

c++ - << C++ 运算符除了移位之外还有什么作用?

javascript - JavaScript 中 <<= 运算符的作用是什么?

flutter - Dart/Flutter 按多个字段对列表进行排序