How to include loop inside transection

i have the following code List<QueryDocumentSnapshot> localList2 = [] ; // inside list i have 5 docs FirebaseFirestore.instance.runTransaction((transaction) async { for (var element in localList2) { DocumentReference userDoc= FirebaseFirestore.instance.collection("users").doc(element.get(‘userId’)); DocumentSnapshot chick = await transaction.get(userDoc); if(chick.exists){ transaction.update(element.get(‘targetDocRef’),{ ‘fieldValue’: ‘value’, }); } } }).then((value){ log.log(‘done’); }).catchError((error){ log.log(error.toString()); }); when i run it i got the following error… Read More How to include loop inside transection

Read and write in a same call in Firestore

async function updateBookingSlots(appointmentID) { let slotId; await firestore .collection("bookingSlots") .where("appointmentID", "==", appointmentID) .get() .then((snapshot) => { snapshot.forEach((doc) => { slotId = doc.id; }); }); await firestore.collection("bookingSlots").doc(slotId).update({ appointmentID: "", patientID: "", requester: "", }); } I’m trying to get the slotId of a booking slot by an appointmentID and then updating the slot. I’m making the… Read More Read and write in a same call in Firestore

Obtain Document ID immediately after you create a new document with Set in Firebase Flutter

In my app I need to get the document ID for a document I just created. I am creating the new document with: db.collection(‘Pack’).doc().set({ ‘userId’: userID, ‘packName’: newPackNameController.text.trim(), ‘description’: newPackDescriptionController.text.trim(), //todo: need to calculate new weight or grab from this pack… ‘totalWeight’: ‘0.0’, ‘weightFormat’: weightFormat, }); Next I need to document ID. I tried this… Read More Obtain Document ID immediately after you create a new document with Set in Firebase Flutter

firestore doc.data is not a function error appears when using docChanges method

I’m building a chat app that and I’d like to listen to doc changes to push new messages as opposed to re-rendering every doc with a change. Firebase has a solution with the following: https://firebase.google.com/docs/firestore/query-data/listen#view_changes_between_snapshots this is also the same solution I found on Stack, but for some reason it’s not working for me. getMessages(docId:… Read More firestore doc.data is not a function error appears when using docChanges method

How to create a reference field in a document from Dart code – Flutter Firebase?

In Cloud Firestore you can create documents with different types of fields, one of them is the Reference field. I want to create a Reference field from Dart code, but it always creates it as a string. I have a collection called "usuarios" (users) and I want to create a subcollection inside that collection called… Read More How to create a reference field in a document from Dart code – Flutter Firebase?

Firestore efficiently inserting documents in subcollections

I’m creating a firestore document that’s not at the root level of an existing collection but somewhat deeper: const response = await addDoc( collection( firestore, ‘stats’, //collection ‘dayStats’, //document ‘years’, //collection ‘2023’, //document ‘january’, //collection ), statData ); At the time of creating an event, the entire subcollection structure might not exist yet, but only… Read More Firestore efficiently inserting documents in subcollections

I would like to prevent writing when there is the same document ID in firestoreb

I would like to prevent writing when there is the same document ID in firestoreb. I am new to flutter. As the title says, when writing from flutter to firestore, if the same document ID exists, it is usually overwritten. So, I would like to prevent the same document ID from being overwritten. Is there… Read More I would like to prevent writing when there is the same document ID in firestoreb

How to remap and add extra property in flutter

everyone; recently I was new in flutter and wanted to try to remap the data from firestore. The data from firestore [ {name:"john",age:12}, {name:"wick",age:12}, ] Expected Result [ {name:"john",age:12,id:xxx}, {name:"wick",age:12,id:xxx}, ] Is this possible to add new property like this, I try to use map but not working for me. Hope someone could help me… Read More How to remap and add extra property in flutter

Deleting document(s) inside a collection with documentReference

I have a collection in firebase called "community" and "events". When an event document is created, a field "communityRef" is included as docRef to community. I’m trying to figure a code that when a community is deleted, all events related to the community are also deleted. building this in flutterflow using custom action onTap on… Read More Deleting document(s) inside a collection with documentReference