在三元运算中检查null [英] Check null in ternary operation
本文介绍了在三元运算中检查null的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如果_contact不为null,我想在文本字段上输入默认值。为此
I wanna put a default value on a textfield if _contact is not null. To do this
new TextField(
decoration: new InputDecoration(labelText: "Email"),
maxLines: 1,
controller: new TextEditingController(text: (_contact != null) ? _contact.email: ""))
有更好的方法吗?例如:Javascript将类似于: text:_contact? _contact.email:
Is there a better way to do that? E.g: Javascript would be something like: text: _contact ? _contact.email : ""
推荐答案
Dart随附?。 / code>和
??
运算符进行空检查。
Dart comes with ?.
and ??
operator for null check.
您可以执行以下操作:
var result = _contact?.email ?? ""
您也可以
You can also do
if (t?.creationDate?.millisecond != null) {
...
}
在JS中等于:
if (t && t.creationDate && t.creationDate.millisecond) {
...
}
这篇关于在三元运算中检查null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文