我在 Dart/flutter 代码中看到了这个一元后缀:?.
像这样:
videoController?.dispose();
我想知道它是如何工作的...
最佳答案
这是 Dart 中的一个很棒的特性
意思是当且仅当该对象不为空时,否则返回空。
简单的例子:
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;
}
还有其他很酷的 null 感知运算符,如 ??
。 Read my QnA了解有关 null 感知运算符的更多信息。
关于dart - dart/flutter 中的这个一元后缀是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51157680/