我想在 flutter 上使用 horizo??ntal_data_table 從 API 呼叫資料。這是出現錯誤的代碼
Container(
width: 200,
height: 52,
padding: const EdgeInsets.fromLTRB(5, 0, 0, 0),
alignment: Alignment.centerLeft,
child: Text(widget.leavelistmodel.response[index].paidLeaveEmployeeNip),
),
結果的資料模型如下所示:
class LeaveListResult {
int? paidLeaveId;
String? paidLeaveEmployeeNip;
String? paidLeaveEmployeeFullName;
}
我已經把它改成了?和 !它仍然有錯誤,我該如何修復錯誤?
uj5u.com熱心網友回復:
我們用于nullSafty模型變數。?
class LeaveListResult {
int? paidLeaveId;
String? paidLeaveEmployeeNip;
String? paidLeaveEmployeeFullName;
}
既然和Swift的optional一樣,nullSafty必須要取消。
Container(
width: 200,
height: 52,
padding: const EdgeInsets.fromLTRB(5, 0, 0, 0),
alignment: Alignment.centerLeft,
child: Text(widget.leavelistmodel.response[index].paidLeaveEmployeeNip ?? 'NULL'),
),
您可以使用 強制展開!,但這不是一個好的做法。原因是當它為空時會出錯。當使用 為 null 時channing,應該顯示例外處理訊息而不是錯誤。
uj5u.com熱心網友回復:
Text不能接受 nullable String?,如果您確定您的資料不是,請null執行以下操作:
child: Text(widget.leavelistmodel!.response[index]!.paidLeaveEmployeeNip),
您還可以為將在以下情況下String?顯示的設定默認值null:
child: Text(widget.leavelistmodel?.response[index]?.paidLeaveEmployeeNip ?? "default value"),
uj5u.com熱心網友回復:
在這種response情況下可以為空。您可以檢查空值,然后繼續。至于paidLeaveEmployeeNip filed 也是一個可為空的字串,但文本小部件不接受空值。您可以直接接受空字串
child: Text("${widget.leavelistmodel.response?[index].paidLeaveEmployeeNip}"),
或為默認值
Text(widget.leavelistmodel.response?[index].paidLeaveEmployeeNip ?? "Got Null")
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/535508.html
標籤:安卓ios扑镖飞镖空安全
下一篇:在Python中比較時間
