[英]What is this unary postfix in dart/flutter?
i saw this unary postfix in Dart/flutter code: ?.
我在Dart / flutter代码中看到了此一元后缀:
?.
like this: 像这样:
videoController?.dispose();
and i want to know how it work... 我想知道它是如何工作的...
This is a great feature in Dart 这是Dart的一大特色
meaning is if and only if that object is not null otherwise, return null . 意思是当且仅当该对象不为null时,否则返回null 。
Simple example: 简单的例子:
void main() {
Person p1 = new Person("Joe");
print(p1?.getName); // Joe
Person p2;
print(p2?.getName); // null
//print(p2.getName); // this will give you an error because you cannot invoke a method or getter from a null
}
class Person {
Person(this.name);
String name;
String get getName => name;
}
There are other cool null aware operators like ??
还有其他一些很酷的null感知运算符,例如
??
. 。 Read my QnA to find out more about null-aware operators.
阅读我的QnA,以了解有关空感知运算符的更多信息。
It tests for null, 它测试是否为空,
https://www.dartlang.org/guides/language/language-tour https://www.dartlang.org/guides/language/language-tour
"?. Conditional member access Like ., but the leftmost operand can be null; example: foo?.bar selects property bar from expression foo unless foo is null (in which case the value of foo?.bar is null)" “?。条件成员访问,类似于。,但是最左边的操作数可以为null;例如:foo?.bar从表达式foo中选择属性bar,除非foo为null(在这种情况下foo?.bar的值为null)”
It is a null-aware operator . 它是一个空值 运算符 。 It is the short form of the following.
这是以下内容的简称。
((obj) => obj == null ? null : x.method())(object)
// is equal to
object?.method()
You can find out more about null-aware operators here . 您可以在此处找到有关空感知运算符的更多信息 。
Reads as: 读为:
Only execute method
if object
is not null
仅当
object
不为null
时才执行method
If object
is null
return null
(otherwise evaluation from method
) 如果
object
为null
返回null
(否则从method
求值)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.