如何在Firestore中为我的阵列/列表添加时间戳?

时间:2019-08-01 17:00:14

标签: flutter google-cloud-firestore

我希望使用用户生成的提交内容在x轴上使用时间生成散点图。为此,我制作了一个地图列表,其中每个地图都是用户提交的,并且包含一个时间戳字段。问题是我无法使用arrayUnion添加时间戳。这篇文章中提到了这个问题:

Link1

  

您不能使用FieldValue.serverTimestamp()作为要联合(添加)或>从文档字段的数组类型值中删除(或从其中删除)的值。如果要>使用该时间戳记值,则需要将其直接传递给您正在更新或设置的字段。

因此,要解决我的问题,我需要在添加一些具有类似于以下形式的数据之后直接更新时间戳字段,而只需添加到我的List / array的最后一个元素/ Map中即可。地图,但我不知道此列表目前有多长时间:

myList [{Map1} {Map2} {Map3} ... {examplefield:'Some data',timstamp:null}]

final Map submissionMap = submissionData.toMap();

//Running a firestore transaction...
    final DocumentReference areaDocument = Firestore.instance.document('Space/SAJJEED');
    Firestore.instance.runTransaction((Transaction tx) async {
      DocumentSnapshot postSnapshot = await tx.get(areaDocument);
      if (postSnapshot.exists) {
//Adds the data except for the timestamp needed
        await tx.update(areaDocument, <String, dynamic>{'$status': FieldValue.arrayUnion([submissionMap])});
        //Insert Code for directly accessing the timestamp field and adding Timestamp 

      }
    });```

Any other workarounds would be appreciated as well.

0 个答案:

没有答案