每当我尝试使用 .orderby("POST_ID", descending: true
时,我都会收到错误“NoSuchMethodError: The getter 'docs' was called on null.Receiver: null Tried Calling: docs”。如果有人对我为什么会收到此错误以及如何解决它有答案,请提供帮助!
这是我的代码:
Container(
margin: EdgeInsets.only(top: 100.0),
child: StreamBuilder(
stream: FirebaseFirestore.instance
.collection("Cities")
.doc(globals.selectedCity)
.collection("Posts")
.orderBy("POST_ID", descending: true)
.where("Category", isEqualTo: globals.selectedCategory)
.snapshots(),
builder: (context, postSnapshot) {
return ListView.builder(
itemCount: postSnapshot.data.docs.length,
itemBuilder: (BuildContext context, int index) {
switch (postSnapshot.data.docs[index]["Type"]) {
case "Image":
return new ImageCard(
imageLink: postSnapshot.data.docs[index]
["ImageLink"],
imageDescription: postSnapshot.data.docs[index]
["ImageDescription"],
posterName: postSnapshot.data.docs[index]
["PosterName"],
);
break;
case "Text":
return new TextCard(
postText: postSnapshot.data.docs[index]
["PostText"],
posterName: postSnapshot.data.docs[index]
["PosterName"],
);
break;
答案 0 :(得分:0)
问题出在这里:
ID_t
您必须首先确保从 firestore 获取数据。在您的情况下,当连接状态正在连接或快照有错误时,您可以在空对象上调用 (postSnapshot.data.docs[index]["Type"])
。考虑使用如下检查构建小部件树:
docs