gpt4 book ai didi

dart - dart/flutter 中的这个一元后缀是什么?

转载 作者:IT王子 更新时间:2023-10-29 06:50:03 25 4
gpt4 key购买 nike

我在 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com