小编典典

在三元运算中检查null

flutter

如果_contact不为null,我想在文本字段上输入默认值。去做这个

new TextField(
    decoration: new InputDecoration(labelText: "Email"), 
    maxLines: 1,
    controller: new TextEditingController(text: (_contact != null) ? _contact.email: ""))

有更好的方法吗?例如:Javascript将类似于:text: _contact ? _contact.email : ""


阅读 267

收藏
2020-08-13

共1个答案

小编典典

Dart附带了?.and ??运算符,用于null检查。

您可以执行以下操作:

var result = _contact?.email ?? ""

你也可以

if (t?.creationDate?.millisecond != null) {
   ...
}

JS中的哪个等于:

if (t && t.creationDate && t.creationDate.millisecond) {
   ...
}
2020-08-13