Check null in triple operation

I want to put the default value in a text box if _contact is not null. For this

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

Is there a better way to do this? For example: Javascript would be something like this:text: _contact ? _contact.email : ""

+4
source share
1 answer

Dart comes with an operator ?.and ??for zero verification.

You can do the following:

var result = _contact?.email ?? ""

You can also do

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

Which is equivalent:

if (t && t.creationDate && t.creationDate.millisecond) {
   ...
}
+6
source

Source: https://habr.com/ru/post/1696058/


All Articles