我想按日期對我的應用程式中的帖子進行排序,但是當我使用 orderby 時出現null check operator used on a null value錯誤。問題是當我輸入“where”和“orderby”時出現問題。
我的代碼如下:
StreamBuilder(
stream: FirebaseFirestore.instance
.collection('posts')
.where('uid', isEqualTo: widget.uid)
.orderBy(
'datePublished',
descending: true,
)
.snapshots(),
builder: (context, snapshot) {
if (snapshot.connectionState == ConnectionState.waiting) {
return const Center(
child: CircularProgressIndicator(),
);
}
return ListView.builder(
primary: false,
shrinkWrap: true,
itemCount: (snapshot.data! as dynamic).docs.length,
itemBuilder: (context, index) {
DocumentSnapshot snap =
(snapshot.data! as dynamic).docs[index];
uj5u.com熱心網友回復:
Firestore 是一個優化的資料庫。因此,您必須為復雜查詢創建復合索引,例如您正在執行的查詢(與 結合orderBy)where才能正常作業。
要解決這個問題,請查看您運行 Flutter 的日志。您應該會看到一個 Firebase 控制臺鏈接,該鏈接將帶您直接創建查詢。創建查詢,等待 5 分鐘或更短的時間使其構建,然后再次嘗試代碼。它應該作業。
uj5u.com熱心網友回復:
因為流可以回傳空值然后快照.data!作為動態錯誤。請檢查 snapshot.hasData
uj5u.com熱心網友回復:
再添加 1 個條件
if (snapshot.data == null) {
return const Center(
child: CircularProgressIndicator(),
);
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/505222.html
標籤:Google Cloud Collective 扑 火力基地 镖 谷歌云火库
上一篇:Firebase資料庫:從孩子那里獲取所有孩子的資料
下一篇:僅在重新渲染后設定狀態
