Check null in ternary operation

前端 未结 1 2063
鱼传尺愫
鱼传尺愫 2021-02-19 00:05

I wanna put a default value on a textfield if _contact is not null. To do this

new TextField(
    decoration: new InputDecoration(labelText: \"Email\"), 
    ma         


        
相关标签:
1条回答
  • 2021-02-19 00:22

    Dart comes with ?. and ?? operator for null check.

    You can do the following :

    var result = _contact?.email ?? ""
    

    You can also do

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

    Which in JS is equal to :

    if (t && t.creationDate && t.creationDate.millisecond) {
       ...
    }
    
    0 讨论(0)
提交回复
热议问题