问题描述
尝试使用 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
解决方法
在 data()
上使用 docs
在 index
的帮助下访问单个文档:
Review.fromJson(snapshot.data.docs[index]!.data()!)
,
改变这个:
Review posts = Review.fromJson(snapshot.data);
为此:
Review posts = Review.fromJson(snapshot.data.docs[index]);