我正在從 firebase 獲取資料并將其顯示在螢屏上,最初,firebase 資料將為空,所以我想顯示文本Press to add data。我嘗試使用 if 條件進行操作,snapshot.data!.docs ==null但沒有奏效,但沒有奏效。一旦用戶將資料添加到 firebase,流構建器將獲取并顯示它,文本將不存在。如何實作。
StreamBuilder(
stream: FirebaseFirestore.instance
.collection('lender')
.doc(auth.currentUser!.email)
.collection('borrowers')
.snapshots(),
builder: (context, AsyncSnapshot<QuerySnapshot> snapshot) {
if (!snapshot.hasData) {
return const Center(
child: CircularProgressIndicator(
backgroundColor: Color(0xff8eacbb),
));
}
if (snapshot.data!.docs ==null) {
return Text(
'Press to add data',
style: TextStyle(fontSize: 20),
);
} else {
return ListView(
children: snapshot.data!.docs.map((data) {
return BorrowerCard(BorrowerName: data['Name']);
}).toList());
}
},
)
我也嘗試顯示文本而不是文本,CircularProgressIndicator但文本只顯示了幾秒鐘,但我希望它顯示直到用戶添加資料。
uj5u.com熱心網友回復:
對于沒有結果的查詢,您將獲得包含空檔案陣列的快照。所以要檢查一下:
if (snapshot.data!.docs.isEmpty) {
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/484122.html
