【发布时间】:2022-01-05 17:10:38
【问题描述】:
我想从我的 Firestore 中读取嵌套字段。字段的层次结构是
cmets
|__ c_id
|__ c_text
|_________回复
|____________ r_id
|____________ r_text
我尝试了以下代码:
var documentSnapshot = await FirebaseFirestore.instance
.collection("article_engagements")
.doc(id.toString())
.get();
var comments = await FirebaseFirestore.instance
.collection("article_engagements")
.doc(id.toString())
.collection("comments")
.get();
comments.docs.forEach((element) {
var replies = element.get('replies');
commentsList.add(Comment(
id: element.get('c_id'),
text: element.get('c_text'),
replies: ,
));
});
article = new ArticleEngagement(
id: id,
likes: documentSnapshot.get('likes'),
shares: documentSnapshot.get('shares'),
comments: commentsList,
);
ArticleEngagement、评论和回复模型如下:
class ArticleEngagement {
int? id;
int? likes;
int? shares;
List<Comment>? comments;
ArticleEngagement({
this.id,
this.likes,
this.shares,
this.comments,
});
}
class Comment {
String? id;
String? text;
List<Reply>? replies;
Comment({
this.id,
this.replies,
this.text,
});
}
class Reply {
String? id;
String? reply;
Reply({
this.id,
this.reply,
});
}
【问题讨论】:
-
您能否编辑您的问题并从您的代码中分享更多信息?专门针对
Comment和ArticleEngagement类,以便拥有minimal code that reproduces the problem -
@RogelioMonter 我已经添加了代码。您可以立即查看
-
您有机会查看我的answer吗?
标签: firebase flutter google-cloud-firestore