How to delete data from Firestore with specific parameter flutter? - flutter

I'm trying to delete some data with where parameter to specify which one should be deleted.
I've this method and it works perfectly.
final FirebaseFirestore _db = FirebaseFirestore.instance;
Future deleteInstructorDocumentInFirestore(String url) async {
_db.collection(instructorsDocument)
.where("photo", isEqualTo: url)
.get()
.then((value) {
for (var element in value.docs) {
_db.collection(instructorsDocument)
.doc(element.id)
.delete();
}
});
}
So I've tried the same thing with this one but it doesn't work.
Firestore
Function
final FirebaseFirestore _db = FirebaseFirestore.instance;
Future deleteEvent(String uid, DateTime from, DateTime to) async {
print("From: $from");
print("From type: ${from.runtimeType}");
print("To: $to");
print("To type: ${to.runtimeType}");
_db.collection(instructorsEvent)
.where("uid", isEqualTo: uid)
.where("from", isEqualTo: from)
.where("to", isEqualTo: to)
.get()
.then((value) {
for (var element in value.docs) {
_db.collection(instructorsEvent)
.doc(element.id)
.delete();
}
});
}
What am I doing wrong.
Thanks in advance

The from and to fields are strings. But you are trying to compare them to dart DateTime object, which are not strings. As such, they will not match in any query - your queries are simply returning no documents at all, so there is nothing to delete. (I suggest adding some debug logging to better observe this for yourself.)
Firestore will not convert DateTime objects to strings for you, nor will it try to guess how to compare them for you. You must provide a similar type of value to the document field you are trying to compare to.
So, if your document fields must remain strings, then you should find a way to convert your DateTime objects to strings before handing them to a Firestore query. Or, you must find another way of representing points in time (in Firestore, usually a timestamp type field), then provide objects to the query that Firestore naturally understand to be comparable to timestamps.
See also:
Flutter: query Firestore document by timestamp
How to query for an entire day with timestamp values in Firestore

Problem Solved
#Doug Stevenson thanks
Utils
static String toDateTimeToStringFromFirestore(DateTime dateTime) {
final date = DateFormat("yyyy-MM-ddTHH:mm:ss.SSS").format(dateTime);
return date;
}
EventViewModel
Future deleteEvent(String uid, DateTime from, DateTime to) async {
final fromString = Utils.toDateTimeToStringFromFirestore(from);
final toString = Utils.toDateTimeToStringFromFirestore(to);
_db.collection(instructorsEvent)
.where("uid", isEqualTo: uid)
.where("from", isEqualTo: fromString)
.where("to", isEqualTo: toString)
.get()
.then((value) {
for (var element in value.docs) {
_db.collection(instructorsEvent)
.doc(element.id)
.delete();
}
});
}

Related

Flutter - Snapshot from Sub-collection

I was reading several answers here but I could not get better understanding to solve my issue which is the following...
I don't know how to get the "docId" in the code below...
I want to query a sub-collection "People" as snapshot for the stream method which I will listen using Bloc...
I am not able to understand how to get the docId. Anyone could help me with this?
Thanks
Stream<People> statusMember({required String email}) {
var user = auth.FirebaseAuth.instance.currentUser;
final snap = _firestore
.collection('users')
.doc(user!.email)
.collection('people') //sub-collection
.doc(docId) // docId as reference? How to get it?
.snapshots()
.map((snapshot) => People.fromSnapshot(snapshot)); // model
print(snap);
return snap;
}
By digging more into the Firebase documentation, it is not possible to read docs in a sub-collection the way I wanted. Therefore, I've changed my approach and the code looks like this:
Stream<People?> statusMember({required String email}) {
var user = auth.FirebaseAuth.instance.currentUser;
final snap = _firestore
.collection('users')
.doc(user!.email)
.collection('people')
.where('email', isEqualTo: email)
.snapshots()
.map(
(snappshot) {
for (var doc in snappshot.docs) {
return People.fromSnapshot(doc);
}
},
);
print(snap);
return snap;
}
it is working as expected.

How to use Array contains in the same array fields in firebase for flutter

I have a chat collection.
each document has an array with two user id's.
my goal is to get the chat that has both user sys id's
I tried running the following but I got an error because we cant use two 'arrayContains' in one query.
Is there any way to perform such query?
here is an image of the data structure
Future getChat({required List userIdsArr}) async {
var docId = '';
userIdsArr.sort((a, b) {
return a.compareTo(b);
});
var filter1 = userIdsArr[0];
var filter2 = userIdsArr[1];
await chat
.where(userIdsArrayColumn, arrayContains: userIdsArr[0])
.where(userIdsArrayColumn, arrayContains: userIdsArr[1])
.get()
.then((value) {
value.docs.forEach((element) {
docId = element.id;
});
});
return docId;
}
the goal is to get the chat that pertains to the users being passed in userIdsArr
this seems to work, is there a better way of doing this?
Future getChat({required List userIdsArr}) async {
var docId = '';
userIdsArr.sort((a, b) {
return a.compareTo(b);
});
await chat
.where(userIdsArrayColumn, arrayContains: userIdsArr[0])
// .where(userIdsArrayColumn, arrayContains: userIdsArr[1])
.get()
.then((value) {
value.docs.forEach((element) {
if (element[userIdsArrayColumn].contains(userIdsArr[1])) {
log('match found!');
docId = element.id;
}
});
});
return docId;
}
A query can only contain a single array-contains query.
To allow your query, you'll want to add an additional field to the document where you keep the pair of UIDs in a predictable (e.g. alphabetical) order. With such a field you can then use a query such as:
where("concatenated_uids", isEqualTo: userIdsArr[0]+"_"+ userIdsArr[1])

How to read Firestore field value in flutter?

I want to get field value.
my code is..
void _checkNumner(String number) async {
final userRef = firestore.collection('users');
var documentSnapshot =
await userRef.where("number", isEqualTo: true).get().then((num) {
QuerySnapshot<Map<String, dynamic>> number = num;
print(number);
print("test");
});
print(documentSnapshot);
}
but my console is
how I get field number?
I want to load number values ​​in all docs.
I'm so beginer. T.T
Please reply from the masters
Thank you
Firebase firestore is a NoSQL, document-oriented database. User Data is stored in documents which are organized into collection , i.e collection contains list of document. In simpler words we can say QuerySnapshot contains/provide group of DocumentSnapshot. more about firestore data model
Collection --> QuerySnapshot --> Group of DocumentSnapshot
Document --> DocumentSnapshot
1) Fetch from collection - QuerySnapshot
Here we'll get list of DocumentSnapshots, we can filter by using where commad
Future<void> checkNumber(int number) async {
final QuerySnapshot snapshot = await FirebaseFirestore.instance
.collection('users')
.where("number", isEqualTo: number)
.get();
snapshot.docs.isEmpty
? {
//TODO: your code here
debugPrint("no data found")
}
: {
for (DocumentSnapshot element in snapshot.docs)
{
//TODO: your code here
debugPrint("number is: ${element['number']}"),
debugPrint("name is: ${element['name']}"),
}
};
}
1) Fetch from document - DocumentSnapshot
To fetch data from document we require documentId, and we get a single documentSnapshot instead of multiple like in above way.
Future<void> checkNumberWithDocId() async {
const String docId = 'aaaa';
final DocumentSnapshot snapshot = await FirebaseFirestore.instance.collection('users').doc(docId).get();
snapshot.exists
? {
//TODO: your code here
debugPrint("no data found")
}
: {
//TODO: your code here
debugPrint("number is: ${snapshot['number']}"),
debugPrint("name is: ${snapshot['name']}"),
};
}

Firebase Reference

How can I reference the collection in the document I auto-id in Firebase?
final CollectionReference _olanaklar5 = _database
.collection("Kategoriler")
.doc("Hoteller")
.collection("5_Yıldızlı")
.doc() //======> here
.collection("Olanaklar");
You can get list all document id with doc.id
List<String> _listDocId = [];
await fireStore
.collection("Kategoriler")
.doc("Hoteller")
.collection("5_Yıldızlı")
.get()
.then((QuerySnapshot querySnapshot) {
for (var doc in querySnapshot.docs) {
_listDocId.add(doc.id);
}
});
and query in list doc id
for (var id in _listDocId) {
final CollectionReference _olanaklar5 = _database
.collection("Kategoriler")
.doc("Hoteller")
.collection("5_Yıldızlı")
.doc(id)
.collection("Olanaklar");
}
If you're using Firebase auth, it's preferred to keep User's UID as doc, else you can use .where() as a query to match the fields in all documents. But as the app scales, it will be a hectic process and will consume many reads.

FIrestore Query Document in Collection Using Single Field/Attribute in Flutter

I am trying to fetch the role of the currently authenticated user stored in users collection. What I am trying to achieve is at login time, query the user role by traversing fetching the user's document in the collection and sifting through the fields or checking all documents and returning the field role as a string.
Collection and document snapshot(excuse the terminology):
All documents in users collection have same fields for now.
Please how do I go about writing this type of query in flutter? I have tried using AuthResult in my service and FirebaseAuth to get current user(but no way to access the fields in the document).
Thanks.
String role;
getUserRoleWithFuture() async {
String currID = await _authService.getCurrentUID();
String mRole;
Firestore.instance.collection(USERS_REF).document(currID).get().then((doc) {
mRole = doc.data['role'];
print(mRole);
});
return mRole;
}
Future<String> getUserRoleWithStream() async {
String currID = await _authService.getCurrentUID();
String sRole;
Firestore.instance
.collection(USERS_REF)
.document(currID)
.snapshots()
.listen((DocumentSnapshot ds) {
if (ds.exists) {
sRole = ds.data['role'];
print('with stream:\t$sRole');
}
});
return sRole;
}
In the method getUserRoleWithStream() I am trying to retrieve the value printed out like role = getUserRoleWithStream() but instead get this in console a value of type Future<String> can't be assigned to a variable of type string.
How do I get this value using either the stream (cos it constantly observes the collection) or using the other method and use it in my widget?
Thanks again.
This is the working solution, in case anyone else runs into this. I appreciate the effort made into helping me understand the issue but here's the answer:
String role;
getUserRoleWithFuture() async {
String currID = await _authService.getCurrentUID();
String mRole;
Firestore.instance.collection(USERS_REF).document(currID).get().then((doc) {
mRole = doc.data['role'];
print(mRole);
});
return mRole;
}
Future<String> getUserRoleWithStream() async {
String currID = await _authService.getCurrentUID();
String sRole;
Firestore.instance
.collection(USERS_REF)
.document(currID)
.snapshots()
.listen((DocumentSnapshot ds) {
if (ds.exists) {
sRole = ds.data['role'];
print('with stream:\t$sRole');
}
});
return sRole;
}
Well first off, I assume the AuthResult.user.uid and your user's collection user's id is same. So that once you have the user from AuthResult, you can query your firestore collection to get the user's role as follows.
Future<String> getUserRole(String uid) async {
DocumentSnapshot ds = await Firestore.instance.collection('users').document(uid).get();
return ds.data['role'];
}