如何解决getter 'docs' 被调用为 null接收者:null 尝试调用: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,),],)
],);
},));
解决方法
第一个流可以发出一个空快照。
您应该在访问文档之前检查快照是否有数据:
if(!snapshot.hasData) {
return Center(child: CircularProgressIndicator());
}
// if it has data,do your thing:
final doc = snapshot.data.docs;
return ListView( ..... )
,
使您的代码正常工作的另一种解决方案是使用 null safety 并将 snapshot.data 替换为 snapshot?.data
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。