我學習 Flutter 已經有一段時間了,試圖從現有的 Flutter 專案中理解,所以后面的代碼不是我的。
目前我正在測驗一個專案,但我面臨一個我從未見過的錯誤。
The getter 'length' was called on null.
Receiver: null
Tried calling: length
我認為它來自這部分代碼。
StreamBuilder<List<DocumentSnapshot>>(
stream: postListBloc.postStream,
builder: (context, snapshot) {
if (snapshot.connectionState == ConnectionState.waiting)
return Center(
child: CircularProgressIndicator(),
);
else {
int presentLength = snapshot.data.length;
return ListView.builder(
physics: NeverScrollableScrollPhysics(),
shrinkWrap: true,
itemCount: snapshot.data.length,
itemBuilder: (context, index) {
DocumentSnapshot documentSnapshot =
snapshot.data[index];
String id = documentSnapshot.id;
debugPrint('${snapshot.data.length}');
return Column(children: [
Padding(
padding: EdgeInsets.only(bottom: 10),
child: PostCardView(
documentSnapshot.get('community'),
id,
true)),
(index != snapshot.data.length - 1)
? Container()
: buildProgressIndicator(presentLength)
]);
});
}
},
),
我在這里搜索了不同的解決方案,但到目前為止沒有任何效果。
如果有人知道如何解決這個問題。
uj5u.com熱心網友回復:
基本上錯誤說您的快照中沒有資料。資料為空,首先檢查它是否不為空,如果它為空,則顯示沒有可用資料,如果它不為空,則顯示資料
uj5u.com熱心網友回復:
您可以通過兩種方式做到這一點:
1-將流生成器中的初始資料設定為一個空串列,如下所示:
initialData : []
2-另一種方法是檢查快照是否有資料:
snapshot.hasData
uj5u.com熱心網友回復:
這在dart語言中稱為null 安全性,因為您說您試圖從現有的 Flutter 專案中理解,問題是這個專案是內置在 Flutter 版本中的,它不支持null 安全性。將flutter版本降級為專案版本。我建議您了解 null 安全性,因為它非常重要。
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/533175.html
標籤:扑
下一篇:如何為文本編輯控制器創建串列