Firebase FieldValue.arrayUnion()在第一次更新/设置时在数组中创建空值

Firebase FieldValue.arrayUnion()在第一次更新/设置时在数组中创建空值,firebase,flutter,google-cloud-firestore,Firebase,Flutter,Google Cloud Firestore,当我使用FieldValue.arrayUnion()更新数组时,在第一次发布到firebase时,我面临一个空值/对象被创建的问题 List commentsList=[]; Future postComment(字符串postId、字符串注释、字符串注释人)异步{ commentsList.add({ “评论”:评论, “commentBy”:commentBy }); 等待FirebaseFirestore.instance.collection('posts').doc(postId).

当我使用FieldValue.arrayUnion()更新数组时,在第一次发布到firebase时,我面临一个空值/对象被创建的问题

List commentsList=[];
Future postComment(字符串postId、字符串注释、字符串注释人)异步{
commentsList.add({
“评论”:评论,
“commentBy”:commentBy
});
等待FirebaseFirestore.instance.collection('posts').doc(postId).collection('comments').doc(postId).更新({
“注释”:FieldValue.arrayUnion(commentsList)
});
}

现在,当我调用此函数发布数据时,数据在firebase上成功创建,但在集合中第一次创建数据时,它也有一个空值

以下是屏幕截图:

函数执行前:

函数执行后:

我还尝试使用.set()方法更新数据并添加SetOptions(merge:true)。正在成功创建和合并数据,但再次创建空元素,如上面的屏幕截图所示


有人知道我做错了什么吗?

这是一个逻辑错误。问题是,在将
querySnapshot
数据映射到我的注释列表时,我正在为
comments
数组创建
null
值。

对此问题表示抱歉。这是我这边的一个逻辑错误。问题是,在将querySnapshot数据映射到注释列表时,我正在为注释数组创建空值。感谢您的回复。感谢您分享解决方案。你能不能也把它作为答案贴出来,让社区从中受益?
List<Map<String, dynamic>> commentsList = [];
Future<void> postComment (String postId, String comment, String commentBy) async{
commentsList.add({
    'comment' : comment,
    'commentBy' : commentBy
  });
  
await FirebaseFirestore.instance.collection('posts').doc(postId).collection('comments').doc(postId).update({
  'comments' : FieldValue.arrayUnion(commentsList)
});