0

尝试使用 StreamBuilder 从 Firestore 获取用户帖子时出现此错误。

@override
Widget build(BuildContext context) {
return Column(
  children: <Widget>[
Expanded(
child: StreamBuilder(
      stream: postRef
          .where("bookmarks",
          arrayContains: FirebaseAuth.instance.currentUser.uid)
          .orderBy('timestamp', descending: true)
          .snapshots(),
      builder: (context, snapshot) {
if (snapshot.hasData) {
        return ListView.separated(
          itemCount: snapshot.data.docs.length,
          itemBuilder: (context, index) {
            internetChecker(context);
            Review posts = Review.fromJson(snapshot.data);
            return Padding(
              padding: const EdgeInsets.only(bottom: 10.0),
              child: Posts(post: posts),
            );},
          separatorBuilder: (context, index) {
            return Column(
              children: [
                if (index % 3 == 0 && index != 0)
                  AdWidget(ad: ad)
              ],);});
} else if (snapshot.hasError) {
  return Center(
    child:  Text(Languages.of(context).errorOcc),
  );
} else {
  return  Container();}},))],);//)]);
}

我尝试将评论帖子转换为 Map<String, dynamic>,但随后 child: Posts(post: posts) 给出了相同的错误。我尝试了答案中的建议,但那是 FutureBuilder,而不是 StreamBuilder。我是新手,所以仍然很困惑。非常感谢帮助!

4

2 回答 2

0

在以下工具的帮助下使用data()ondocs访问单个文档index

Review.fromJson(snapshot.data.docs[index]!.data()!)
于 2021-07-28T07:36:34.590 回答
0

改变这个:

Review posts = Review.fromJson(snapshot.data);

对此:

Review posts = Review.fromJson(snapshot.data.docs[index]);
于 2021-07-28T07:36:54.167 回答