![](/img/trans.png)
[英]The getter 'email' was called on null. Receiver: null Tried calling: email
[英]The getter 'docs' was called on null. Receiver: null Tried calling: docs
當我運行我的應用程序時,總是會出現一個錯誤,即:
The getter 'docs' was called on null.
Receiver: null
Tried calling: docs
但一切正常,我該如何解決? 我認為 flutter 的更新導致此代碼錯誤,因為我遇到了很多這樣的問題。 我的代碼中的重要部分:
return StreamBuilder(
stream: FirebaseFirestore.instance
.collection('medicine')
.where('userId', isEqualTo: user.uid)
.snapshots(),
builder: (context, snapshot) {
final doc = snapshot.data.docs;
return ListView.builder(
itemCount: doc.length,
itemBuilder: (ctx, index) {
if (snapshot.data == null) {
return Center(
child: CircularProgressIndicator(),
);
}
return Container(
child: Stack(
children: [
Row(
children: [
Flexible(
child: Column(
children: [
Text(
'اسم الدواء: ' + doc[index]['medicationName'],
textAlign: TextAlign.start,
),
],
),
)
],
),
],
),
);
},
));
第一個 stream 可以發出一個空快照。
您應該在訪問文檔之前檢查快照是否有數據:
if(!snapshot.hasData) {
return Center(child: CircularProgressIndicator());
}
// if it has data, do your thing:
final doc = snapshot.data.docs;
return ListView( ..... )
使您的代碼工作的另一種解決方案是使用null 安全並將 snapshot.data 替換為 snapshot?.data
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.