我不明白。
builder: (context, snapshot) {
if (snapshot.data != null && widget.stored!.b) {
return new GridView.count(
children: List.generate(snapshot.data!.length, (index) {
return _buildCell(snapshot.data![index]);
}));
}
});
所以,回顧一下:
if (snapshot.data != null && widget.stored!.b)
這個片段似乎是錯誤所在。我正在嘗試獲取 'b' 并僅顯示那些帖子(如果為真)。
我的 Stored.dart 中有這個:
class Stored extends Base {
late bool b;
}
如您所見,我正在嘗試檢查字串是否為“1”,如果是,則為其分配 true:
Stored(Map<String, dynamic> json) : super(json) {
post-number = json["posts"];
bool b = post-number == "1";
}
JSON 回應為:
{"posts":"1"}';
我不明白我做錯了什么?我試圖只顯示其中編號為 1 的帖子,但是在嘗試獲取時,Null check operator used on a null value出現錯誤。這是為什么?
uj5u.com熱心網友回復:
錯誤意味著widget.stored是null。該!告訴編譯器,它不應該是空。因此,請改用條件屬性訪問:
widget.stored?.b == true
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/359670.html
