如何使用Flutter更新Cloud Firestore上文档的字段?

时间:2019-10-14 19:31:37

标签: firebase flutter dart google-cloud-firestore

我尝试更新Firestore数据库字段。

  Future<void> approveJob(String categoryId) {

注释行已在数据库上更新。但是我硬编码uid。不用存储就可以获取uid吗?

   //return _db.collection('jobs').document('25FgSmfySbhEPe1z539T').updateData({'isApproved':true});


   return _db
        .collection('jobs')
        .where("categoryId", isEqualTo: categoryId)
        .getDocuments()
        .then((v) {
          try{
            v.documents[0].data.update('isApproved', (bool) => true,ifAbsent: ()=>true);

// No Errors. But not updating

         }catch(e){
            print(e);
          }
    });
  }

4 个答案:

答案 0 :(得分:2)

===2020 年 12 月===

在 Flutter 中有两种更新 Firestore 文档的方法:

  1. update() - 在文档上设置数据,覆盖任何现有数据。如果该文档尚不存在,则会创建该文档。
  2. FirebaseFirestore.instance.collection('collection_name').doc('document_id').update({'field_name': 'Some new data'}); - 更新文档上的数据。数据将与任何现有文档数据合并。如果尚不存在文档,则更新将失败。

因此,要更新现有文档中的确切字段,您可以使用

 "SeatLayoutDetails": {
  "LayoutFlag": 1,
  "SeatLayout": {
    "SeatColumns": [
      {
        "Seats": [
          {
            "SeatNo": "L1",   
            "SeatTypeId": 5,                
            "BerthType": "L",
          
          },
          {
            "SeatNo": "L2",
            "SeatTypeId": 5,
            "BerthType": "L",
          
          },
          null,
          {
            "SeatNo": "L3",
            "SeatTypeId": 5,
            "BerthType": "L",
         
          },
          null
        ]
      },
      {
        "Seats": [
          {
            "SeatNo": "L4",
            "SeatTypeId": 5,
            "BerthType": "L",
          
          },
          {
            "SeatNo": "L5",
            "SeatTypeId": 5,
            "BerthType": "L",
          
          },
          null,
          {
            "SeatNo": "L6",
            "SeatTypeId": 5,
            "BerthType": "L",
          
          },
          null
        ]
      },
      {
        "Seats": [
          {
            "SeatNo": "L7",
            "SeatTypeId": 5,
            "BerthType": "L",
        
          },
          {
            "SeatNo": "L8",
            "SeatTypeId": 5,
            "BerthType": "L",
           
          },
          null,
          {
            "SeatNo": "L9",
            "SeatTypeId": 5,
            "BerthType": "L",
            
          },
          null
        ]
      },
      {
        "Seats": [
          {
            "SeatNo": "L10",
            "SeatTypeId": 5,
            "BerthType": "L",
           
          },
          {
            "SeatNo": "L11",
            "SeatTypeId": 5,
            "BerthType": "L",
          
          },
          null,
          {
            "SeatNo": "L12",
            "SeatTypeId": 5,
            "BerthType": "L",
           
          },
          null
        ]
      },
      {
        "Seats": [
          {
            "SeatNo": "L13",
            "SeatTypeId": 5,
            "BerthType": "L",
         
          },
          {
            "SeatNo": "L14",
            "SeatTypeId": 5,
            "BerthType": "L",
           
          },
          null,
          {
            "SeatNo": "L15",
            "SeatTypeId": 5,
            "BerthType": "L",
           
          },
          null
        ]
      },
      {
        "Seats": [
          null,
          {
            "SeatNo": "U16",
            "SeatTypeId": 5,
            "BerthType": "U",
        
          },
          {
            "SeatNo": "U17",
            "SeatTypeId": 5,
            "BerthType": "U",
          
          },
          null,
          {
            "SeatNo": "U18",
            "SeatTypeId": 5,
            "BerthType": "U",
       
          },
          null
        ]
      },
      {
        "Seats": [
          null,
          {
            "SeatNo": "U19",
            "SeatTypeId": 5,
            "BerthType": "U",
         
          },
          {
            "SeatNo": "U20",
            "SeatTypeId": 5,
            "BerthType": "U",
           
          },
          null,
          {
            "SeatNo": "U21",
            "SeatTypeId": 5,
            "BerthType": "U",
        
          },
          null
        ]
      },
      {
        "Seats": [
          null,
          {
            "SeatNo": "U22",
            "SeatTypeId": 5,
            "BerthType": "U",
          
          },
          {
            "SeatNo": "U23",
            "SeatTypeId": 5,
            "BerthType": "U",
           
          },
          null,
          {
            "SeatNo": "U24",
            "SeatTypeId": 5,
            "BerthType": "U",
        
          },
          null
        ]
      },
      {
        "Seats": [
          null,
          {
            "SeatNo": "U25",
            "SeatTypeId": 5,
            "BerthType": "U",
        
          },
          {
            "SeatNo": "U26",
            "SeatTypeId": 5,
            "BerthType": "U",
          
          },
          null,
          {
            "SeatNo": "U27",
            "SeatTypeId": 5,
            "BerthType": "U",
     
          },
          null
        ]
      },
      {
        "Seats": [
          null,
          {
            "SeatNo": "U28",
            "SeatTypeId": 5,
            "BerthType": "U",
        
          },
          {
            "SeatNo": "U29",
            "SeatTypeId": 5,
            "BerthType": "U",
        
          },
          null,
          {
            "SeatNo": "U30",
            "SeatTypeId": 5,
            "BerthType": "U",
          
          },
          null
        ]
      }
    ]
  },

答案 1 :(得分:1)

首先,您需要确定并定位您要修改的字段。

final code = Code.fromSnapshot(document);

FirebaseFirestore.instance.collection('collection_Name').doc('doc_Name').collection('collection_Name').doc(code.documentId).update({'redeem': true});

在这种情况下,code.documentId 代码是包含快照的实例。通过使用它,可以访问“标识符”documentId;然后字段 redeem 更改为 true,

答案 2 :(得分:0)

要更新文档中的字段,您只需遵循this文档。

关于如何获取documentID,您可以按照here

中的说明进行操作

还请记住,您可以update data with transactions

让我知道这是否有帮助。

答案 3 :(得分:0)

  • 更新文档中的值:

    var collection = FirebaseFirestore.instance.collection('collection');
    collection 
        .doc('doc_id') 
        .update({'key' : 'value'}) // <-- Updated data
        .then((_) => print('Success'))
        .catchError((error) => print('Failed: $error'));
    
  • 更新文档中的嵌套值。

    var collection = FirebaseFirestore.instance.collection('collection');
    collection 
        .doc('doc_id')
        .update({'key.foo.bar' : 'nested_value'}) // <-- Nested value
        .then((_) => print('Success'))
        .catchError((error) => print('Failed: $error'));
    
  • 向现有文档添加新值。

    var collection = FirebaseFirestore.instance.collection('collection');
    collection
        .doc('doc_id')
        .set(yourData, SetOptions(merge: true)); // <-- Set merge to true.