类型 '_JsonQuerySnapshot' 不是 StreamBuilder 中类型 'Map<String, dynamic>' 的子类型

问题描述

尝试使用 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,但随后子项:Posts(post: posts) 给出了相同的错误。我尝试了 answers 的建议,但那是 FutureBuilder,而不是 StreamBuilder。我对此很陌生,所以仍然很困惑。非常感谢帮助!

解决方法

data() 上使用 docsindex 的帮助下访问单个文档:

Review.fromJson(snapshot.data.docs[index]!.data()!)
,

改变这个:

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

为此:

Review posts = Review.fromJson(snapshot.data.docs[index]);