【问题标题】:How to update map data in Array? Flutter firebase如何更新数组中的地图数据?颤振火力基地
【发布时间】:2020-08-12 22:27:52
【问题描述】:

我使用颤振。我想仅在 2020 年 4 月 26 日这一周在 Firebase 中将 CH 的值更新为 false。什么时候有办法?

Ex.cord 失败:

_firestore.collection('member').document(uidMember[index])
.updateData({'statistics': [{'${submit}': val,}]});

【问题讨论】:

  • 读取文档,修改内存中的数组,然后写回文档。
  • 你能提供一些代码吗?您在哪里尝试阅读数据以及与此问题相关的任何内容?
  • 我不知道获取有关该部分的数据。 Ex._firestore.collection('member').document(uidMember[index]) .updateData({'statistics': [{'${submit}': val,}]}); X 失败

标签: arrays firebase flutter dart google-cloud-firestore


【解决方案1】:

鉴于您共享的 Firestore sn-p,您可以执行以下代码来更新数据:

var ref = _firestore.collection('member').document(uidMember[index]);
ref.get() => then(function(snapshot) {
    List<dynamic> list = List.from(snapshot.data['statistics']);
    //if you need to update all positions of the array do a foreach instead of the next line
    list[0].CH = false;
    ref.updateData({
        'statistics': list
    }).catchError((e) {
        print(e);
    });
}.catchError((e) {
    print(e);
});

注意:我尚未对此进行测试,因此您可能需要对其进行调整,但应该是一个很好的起点。

【讨论】:

    【解决方案2】:

    首先创建一个List,其中包含更新的项目。使用列表创建一个Map,然后将此映射更新到 Cloud Firestore:

    List<Map<String, dynamic>> updatedList = [...];
    Map<String, dynamic> updatedData = {
      'statistics': updatedList,
    };
      
    var collection = FirebaseFirestore.instance.collection('collection');
    collection 
        .doc('doc_id')
        .update(updatedData);
    

    【讨论】:

      猜你喜欢
      • 2021-02-03
      • 2021-06-05
      • 2021-03-10
      • 2021-07-03
      • 2020-07-28
      • 2021-03-19
      • 2016-09-17
      • 1970-01-01
      • 2020-06-26
      相关资源
      最近更新 更多