dart 如何从FlutterFlow的Google Cloud Firestore中的Map结构中删除对象?

k3bvogb1  于 2023-05-26  发布在  Flutter
关注(0)|答案(1)|浏览(239)

我在查询一个集合并获取Map结构中的数据,就像这样-

eventData : [{
   eventUID: rQigoeThdP0thE9oChld,
   eventLocation: Bldg-4,
   eventName: Test Prod 0217,
   eventStatus: Complete,
   eventContacts: [{                    
   contactName: Anna Leochal,
   contactTitle: Station Dir,
   contactUID: L2z9Jhr3i49YVBLzA4ta
   }, {                 
   contactName: Eleonore Hendricks,
   contactTitle: Station Manager,
   contactUID: ArklRd6Ggivcvi9OMHvj
   }, {                 
   contactName: Tom Warner,
   contactTitle: Station Asst Mgr,
   contactUID: 0bfGw5kaYgseKTxshbMW
   }
  ],
   eventDateModified: Timestamp(seconds = 1676668006, nanoseconds = 962000000),
   eventDateCreated: Timestamp(seconds = 1676667859, nanoseconds = 944000000)
  }
]

我想做的是从由传递的参数myContactUID标识的eventContacts部分中删除数据。例如,传入myContactUID =“0 bfGw 5 kaYgseKTxshbMW”将从eventConcats中删除Tom Warner的数据。
我就是在这里被困住的。我需要从eventContacts中删除与Tom的contactUID(contactName,contactTitle,contactUID)关联的整个对象
如果我将eventContacts转换为List,即。final List myEventContacts = document.get(FieldPath(const [“eventContacts”]))!作为列表;我得到-

myEventContacts: [{                 
   contactName: Anna Leochal,
   contactTitle: Station Dir,
   contactUID: L2z9Jhr3i49YVBLzA4ta
   }, {                 
   contactName: Eleonore Hendricks,
   contactTitle: Station Manager,
   contactUID: ArklRd6Ggivcvi9OMHvj
   }, {                 
   contactName: Tom Warner,
   contactTitle: Station Asst Mgr,
   contactUID: 0bfGw5kaYgseKTxshbMW
   }
  ]

我试过各种方法-

var eventUpdate = {};
eventUpdate[ eventConcats.contactUID : {[myContactUID]: firebase.firestore.FieldValue.delete();}];
db.collection('events').doc('$myEventDocID').set(eventUpdate);

因此─

WriteBatch myEventDocBatch = FirebaseFirestore.instance.batch();
  db
      .collection('events')
      .where('eventUID', isEqualTo: myEventUID)
      .get()
      .then((querySnapshot) {
    var qsEventsLen = querySnapshot.docs.length;
    var qsEventsSize = querySnapshot.size;
    print('qsEventsLen: $qsEventsLen    ');
    print('qsEventsSize: $qsEventsSize    ');

    querySnapshot.docs.forEach((document) {
      // do something with myBatch
      // myEventDocBatch.delete(document.reference);

      var myEventDocID = document.id; // i.e. 6eWyqjfEdG4ZJlepoHSf
      print('myEventDocID: $myEventDocID');

      var myEventUID = document.get(FieldPath(const ["eventUID"]))!
          as String; // i.e. rQigoeThdP0thE9oChld
      print('myEventUID: $myEventUID');

      final eventData = document.data();
      print("eventData: ${document.data()}");

      final List myEventContacts =
          document.get(FieldPath(const ["eventContacts"]))! as List;
      print("myEventContacts: ${myEventContacts}");

      var eventUpdate = {};
      eventUpdate[ eventConcats.contactUID : {[myContactUID]: firebase.firestore.FieldValue.delete();}];
      db.collection('events').doc('$myEventDocID').set(eventUpdate);

    });
    return myEventDocBatch.commit();
  });
  print('End: Get Event Doc');

但毫无结果。我所做的调查似乎对这件事都没有帮助。我只是更困惑了。
任何想法将是最受欢迎的
以前的研究:Cloud Firestore: Update fields in nested objects with dynamic key
Firebase firestore nested query
Query map object data in dart/flutter based on key value
How to delete object from array in firestore

ghhkc1vu

ghhkc1vu1#

我想明白了必须将myEventContacts List转换为Map并迭代值,然后删除索引处找到的项。我不认为这是最有效的路线,所以,如果你有一个想法,如何简化这一点,我很乐意看到它!否则,我希望这对别人有帮助。

querySnapshot.docs.forEach((document) async {

      var myEventDocID = document.id; // i.e. 6eWyqjfEdG4ZJlepoHSf
       print('myEventDocID: $myEventDocID');

      var myEventUID = document.get(FieldPath(const ["eventUID"]))!
          as String; // i.e. rQigoeThdP0thE9oChld
       print('myEventUID: $myEventUID');

      final eventData = document.data();
       print("eventData: ${document.data()}");

      final List myEventContacts =
          document.get(FieldPath(const ["eventContacts"]))! as List;
       print("myEventContacts: ${myEventContacts}");
      
      // Convert myEventContacts to a Map
      final mapMyContact = myEventContacts.asMap();
       print("z mapMyContact: ${mapMyContact}");

      final lenMapMyContact = mapMyContact.length;
       print("lenMapMyContact: $lenMapMyContact");

      // remove item from myEventContacts Map
      int idxContactUID = 99999999;
      for (int i = 0; i < mapMyContact.length; i++) {
        if (mapMyContact[i].toString().contains('$myContactUID')) {
          idxContactUID = i;
           print("- Found contactUID [$myContactUID] in mapMyContact at index $idxContactUID");

          myEventContacts.removeAt(idxContactUID);
           print("- myEventContacts: ${myEventContacts}");
          final docRef = db.collection('events').doc(myEventDocID);
          await docRef.update({'eventContacts': myEventContacts});
        }
      }

      if (idxContactUID == 99999999) {
        print("contactUID [$myContactUID] NOT FOUND in mapMyContact");
      }
    }

相关问题