【发布时间】:2021-02-24 00:18:35
【问题描述】:
我正在尝试检索所有喜欢评论的 uid,以显示页面上的人员列表。我在使用 StreamBuilder 查询此数据时遇到问题。我已经尝试了很多不同的方法,我在这里看到过,但我没有任何运气。在此之前我得到了不同的错误或根本没有数据。谁能帮我解决这个问题?
这是数据库路径 /cmets/411f47a0-8404-4800-a32e-35c260d7b670/cmets/3ygnNwXM3WQlmoNDV6Ac
所以 cmets/postId/cmets/ 那么评论数据就在这里,其中包含每个 uid 的“likers”数组
现在,我收到此错误。它指向返回 StreamBuilder 线。
type 'QuerySnapshot' is not a subtype of type 'DocumentSnapshot'
这是我拥有 StreamBuilder 的小部件。
@override
Widget build(BuildContext context) {
return StreamBuilder(
stream: Firestore.instance
.collection("comments")
.document(widget.postId)
.collection("comments")
.where("likers", arrayContains: widget.uid)
.snapshots(),
builder: (context, snapshot) {
if (snapshot.hasData) {
var user = User.fromDocument(snapshot.data);
return Padding(
padding: const EdgeInsets.only(top: 5.0, left: 5.0, right: 5.0),
child: ClipRRect(
borderRadius: BorderRadius.circular(15.0),
child: Card(
child: ListTile(
tileColor: R.colors.grey200,
leading: user.photoUrl.isNotEmpty
? CachedNetworkImage(
placeholder: (context, url) => CircleAvatar(
backgroundColor: R.colors.grey,
radius: 25.0,
),
imageUrl: user.photoUrl,
width: 50.0,
height: 50.0,
fit: BoxFit.cover,
)
: Icon(
Icons.account_circle,
size: 50.0,
color: R.colors.grey,
),
title: Text(
(user.username),
style: TextStyle(
color: R.colors.black, fontWeight: FontWeight.bold),
),
subtitle: Text(
(user.profileName),
style: TextStyle(color: R.colors.black),
),
trailing: FlatButton(
color: R.colors.blueAccent,
shape: RoundedRectangleBorder(
borderRadius:
BorderRadius.all(Radius.circular(25.0))),
clipBehavior: Clip.hardEdge,
onPressed: () {
if (!following) {
controlFollowUser();
} else {
controlUnfollowUser();
}
},
child: Text(following
//currentUser.following.contains(user.id)
? "Unfollow"
: "Follow")),
),
),
),
);
} else {
return ListTile(
title: Text("Nothing here"),
);
}
});
}
这一行正在获取用户数据,如用户名和个人资料图片,它能够获取这些数据,因为它会从查询的评论数据中获得正确的 uid。这是它的起源。
var user = User.fromDocument(snapshot.data);
factory User.fromDocument(DocumentSnapshot doc) {
if (doc != null) {
return User(
following: doc['following']?.cast<String>() ?? [],
followers: doc['followers']?.cast<String>() ?? [],
id: doc.documentID,
email: doc['email'] ?? "",
username: doc['username'],
photoUrl: doc['photoUrl'],
url: doc['photoUrl'],
profileName: doc['profileName'],
bio: doc['bio'],
createdAt: doc['createdAt'],
talkingTo: doc['talkingTo'],
receiverName: doc['receiverName'],
);
} else {
return new User();
}
}
任何帮助将不胜感激。
【问题讨论】:
-
请编辑问题以显示此错误发生在哪一行代码上,并说明您期望它执行的操作。请务必显示所有相关代码。我们看不到
User.fromDocument()是什么。 -
@DougStevenson 刚刚编辑了它
-
我们仍然看不到
User.fromDocument是什么。你所做的只是再次指出那条线。如果那是带有代码的函数,请显示该代码。 -
我再次编辑并添加了来自 user.dart 文件 @DougStevenson 的代码
标签: firebase flutter dart google-cloud-firestore stream-builder