Unable to fetch document in flutter firestore - flutter

I have a field with name boughtBy, it contains all the ids of the user who have bought the coupons. So, I am trying to show only those coupons which don't have the currentUsers id in the field boughtBy which is an array.
My Code:-
QuerySnapshot snapshotForCoupons=await couponsReference.where('visibility',isEqualTo: true).where('boughtBy',whereNotIn: [currentUser.id]).limit(10).get();
but the problem is it is still showing the coupon which contains the currentUser id.

According to the documentation your query should be written like this:
QuerySnapshot snapshotForCoupons = await couponsReference.where('visibility',isEqualTo: true).where('boughtBy', 'not-in', [currentUser.id]).limit(10).get();

Related

Flutter firestore query on document array field

I've got this firestore database.
There is a 'users' collection containing documents of users. Each user has their own information such as name and a cart. I would like to get all the 'carts' of a particular user in a stream. How would you go about with the query?
await FirebaseFirestore.instance.Collection("users").doc("docIdHere").get().then((.
document) {
//it will get the first item in the cart with index zero
document.get("cart")[0];
//then do what you want
});

Flutter Firebase delete a document of a subcollection using the document ID

Apparently, I was able to retrieve a subcollection data from firebase by using the code below
FirebaseFirestore.instance.collectionGroup('announcementlist').where('id', isEqualTo: '${docID}');
However, I am now trying to delete a subcollection by using this collectionGroup method and it is not working. I want all admin user to be able to delete the subcollections.
So what I found online is something similar to this line of code.
// define document location (Collection Name > Document Name > Collection Name >)
var docRef = Firebase.firestore().collection("Rooms").doc("bsYNIwEkjP237Ela6fUp").collection("Messages");
// delete the document
docRef.doc("lKjNIwEkjP537Ela6fhJ").delete();
which I got from codegrepper but as far as I know, the doc("bsYNIwEkjP237Ela6fUp") part requires the id of the user which is not the method I want because all admin users should be able to delete the data and it uses collection instead of collectiongroup
Is there any solution for this ? Please help.
FirebaseFirestore.instance.collection("Rooms").doc(snapshot.requireData.docs[i].id).collection("Messages").doc(snapshot.requireData.docs[i].id).delete();

How to do access and get data from a subcollection from Firestore in Flutter using Dart?

What code do I write in order to retrieve data from a subcollection in Firestore?
These are my collections and subcollection and the data fields in the subcollection document. I don't know the code for how to access this data from the document in the Running subcollection? Please help!
enter image description here
enter image description here
enter image description here
hey it's very easy to retrive subcollection :
Like this fuction
getData() async {
return await FirebaseFirestore.instance
.collection("Collectionname").doc("doc").collection("subcollection")
.snapshots();
}

Update FireStore Document Id Using Flutter

I want to update firestore document Id Check This
First, you cannot update the id of a document in Firestore. You will have to copy your document with the new id and delete the old one.
But why do you want to do this, especially within the Users collection? You will lose the link between the Firebase Auth Users and the Users details in your Firestore database.
You can't update the document id directly. For that you first need to make a new document with your updated Id and then delete the previous one.
First of all you need to get the document and store the snapshot value in the variable document.
DocumentSnapshot snapshot= await Firestore.instance.collection('Users').document('yourId').get();
var document=snapshot.data;
After that you can
Firestore.instance.collection('Users').document('newId').setData(document);
Firestore.instance.collection('Users').document(document.id).delete();

Flutter / FireStore : Get the ID of the document just created in Collection

I am adding data in Firestore with User > Accomodation collection. Firestore is automatically creating a new document in the collection Accomodation with all the details I haved added. But now I need to get the id of the document that he has just created to pass it to another screen. Should I create my own Accomodation ID or is there a way to get it ?
FirebaseFirestore.instance
.collection('Users')
.doc(uid)
.collection('Accomodation')
.add(data);
The User can have multiple Accomodation already created, I need to get the one just created.
after adding a document you will get a document reference, with this reference you can get id, i don't know is that method names changed or not after new patches but solution must be similar to that
DocumentReference docRef = await
FirebaseFirestore.instance
.collection('Users')
.doc(uid)
.collection('Accomodation')
.add(data);
print(docRef.documentID);