簡體   English   中英

如何更改 flutter 中的 null 字值?

[英]How to change null word value in flutter?

我從 json 文件導入數據,如果 number_phone 的值為空或 null,我如何用無電話號碼替換 null 字? 我的代碼:


Widget listcard(
   String bg, String nom, String address, String temps, dynamic number_phone) {
 void _launchCaller(dynamic number) async {
   var Url = "tel:${number.toString()}";
   if (await canLaunch(Url)) {
     await launch(Url);
   } else {
     throw 'could not place call';
   }
 }
....
....
           child: RaisedButton.icon(
             onPressed: () {
               _launchCaller(number_phone);
             },
             shape: RoundedRectangleBorder(
                 borderRadius: BorderRadius.all(Radius.circular(10.0))),
             label: Text(
               number_phone == null || number_phone == '' ? 'No phone number' : '$number_phone',
               style: TextStyle(
                   color: Colors.white,
                   fontSize: 15,
                   fontWeight: FontWeight.w800),
             ),
             icon: Icon(Icons.call, color: Colors.white, size: 36.0),
             splashColor: Colors.red,
             color: Colors.green[400],
        ....
        ....

圖片解釋 null 字當沒有號碼電話成立時 RaisedButton 圖片當號碼電話是 null 或空

空或 null 就是這樣。 重要的是下面的代碼可以幫助你

'${number_phone.isEmpty ? '' : number_phone}'
'${number_phone ?? ''}'

第一行是空的,第二行是 null

嘗試這樣的事情:

number_phone?? 'No phone number'

或者:

number_phone == null || number_phone == ''? 'No phone number': '$number_phone',

最后我修復它只是添加? json 指令中的“沒有電話”如下所示:

'${data[i]["téléphone"]?? '沒有電話'}'

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM