How to handle likes with Firestore in flutter - flutter

My goal is to handle "likes" on items in my app like so: a given user can toggle his like on a given item.
So I created a collection "Likes" with two properties:
adId as a string
userId as a string
For a userId to like an adId consists in adding a document in the collection.
But because having multiple (adId, userId) document is nonsense, I'd like to handle the unicity of the document.
How to do this efficiently?
What I initially planned to do is write a toggle() function like so:
class Ad {
/// The ad ID
String id;
/// Toggle like and returns the document [id].
void toggle({required String userId}) async {
// Don't let users like their own ads
//if (ad.ownerUid == loggedUser?.uid) return null;
// = Actually store the document
final snapshots = await FirebaseFirestore.instance
.collection(collectionName)
.where("adId", isEqualTo: ad.id)
.where("userId", isEqualTo: userId)
.get();
final docs = snapshots.docs;
if (docs.isEmpty) {
// = Actually store the document
await FirebaseFirestore.instance
.collection(collectionName)
.add({"adId": ad.id, "userId": userId, "on": Timestamp.now()});
} else {
for (final doc in docs) {
await FirebaseFirestore.instance
.collection(collectionName)
.doc(doc.id)
.delete();
}
}
}
}

Related

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']}"),
};
}

How to get a subcollection from a document firestore flutter

Im trying to fetch the documents from a subcollection which is in another document, and when I try to fetch the docs and fill a local list with the "docs data" it doesn't fill it, can anyone tell me what I'm doing wrong here?
My method of when I try to fetch the subcollection:
static Stream<List<CheckInOutModel>> employeeCheckInOutStream() {
return firebaseFirestore
.collection('employees')
.doc(auth.currentUser!.uid)
.collection('employeeList')
.snapshots()
.asyncMap((QuerySnapshot querySnapshot) {
final List<CheckInOutModel> employeesCheckInOutList = [];
for (final element in querySnapshot.docs) {
firebaseFirestore
.collection('employees')
.doc(auth.currentUser!.uid)
.collection('employeeList')
.doc(element.id)
.collection('checkInOutList')
.snapshots()
.asyncMap((QuerySnapshot query) {
for (final element in query.docs) {
final employeeCheckInOutModel =
CheckInOutModel.fromDocumentSnapshot(
documentSnapshot: element,
);
employeesCheckInOutList.add(employeeCheckInOutModel);
}
});
}
return employeesCheckInOutList;
});
}
My method when I fetch the fields of the documents that the subcollection is in:
static Stream<List<EmployeeModel>> employeeStream() {
return firebaseFirestore
.collection('employees')
.doc(auth.currentUser!.uid)
.collection('employeeList')
.snapshots()
.map((QuerySnapshot query) {
final List<EmployeeModel> employees = [];
for (final employee in query.docs) {
final employeeModel =
EmployeeModel.fromDocumentSnapshot(documentSnapshot: employee);
employees.add(employeeModel);
}
return employees;
});
}
So I figured out what I did wrong here, I tried to call a stream of it when I only needed it when a callBack is called, so I changed the logic accordingly and went with Future instead Stream
My updated code:
static Future<List<CheckInOutModel>> employeeCheckInOutStream({
required String id,
}) async {
final List<CheckInOutModel> employeesCheckInOutList = [];
final query = await firebaseFirestore
.collection('employees')
.doc(auth.currentUser!.uid)
.collection('employeeList')
.doc(id)
.collection('checkInOutList')
.get();
for (final employee in query.docs) {
final employeeCheckInOutModel = CheckInOutModel.fromDocumentSnapshot(
documentSnapshot: employee,
);
employeesCheckInOutList.add(employeeCheckInOutModel);
}
return employeesCheckInOutList;
}

How to filter Firestore documents using Flutter?

I follow this tutorial to create a chat. The code to insert message into Firestore is the follow
var _firebaseRef = Firestore.instance.collection("chats");
void sendMessage(String message, String idFrom, ProductModel productModel,
String fromName) {
String groupChatId;
String idTo = productModel.userId;
if (idFrom.hashCode <= productModel.userId.hashCode) {
groupChatId = '$idFrom-$idTo';
} else {
groupChatId = '$idTo-$idFrom';
}
var documentReference = _firebaseRef
.document(groupChatId)
.collection(groupChatId)
.document(DateTime.now().millisecondsSinceEpoch.toString());
Firestore.instance.runTransaction((transaction) async {
await transaction.set(
documentReference,
{
'message': message,
'idFrom': idFrom,
"productId": productModel.id,
"createdAt": utils.currentTime(),
'timestamp': DateTime.now().millisecondsSinceEpoch.toString(),
"fromName": fromName,
"idTo": productModel.userId
},
);
});
}
As you can see the groupChatId to be used on DocumentId and CollectionId is a composition of 2 ids (sender and receiver).
This is what its looks like:
The code to displey each message works fine
String groupChatId;
if (idFrom.hashCode <= idTo.hashCode) {
groupChatId = '$idFrom-$idTo';
} else {
groupChatId = '$idTo-$idFrom';
}
return _firebaseRef
.document(groupChatId)
.collection(groupChatId)
.orderBy('timestamp', descending: true)
.snapshots();
In that picture you can see 1 chat with 1 message, I am trying to display each chat of my current user, to do that I 'd to filter all document inside of chat collection, so the user can click it and get list message on chatd detail page.
I don't know how to do that, if you have a better approach to do the chat I will appreciate it
You can call getDocuments and then methods, inside of it you can make your validations, on this case you can if the documentIDcontains your ID. Like this
Firestore.instance.collection("chats").getDocuments().then((value) {
print(value);
value.documents.forEach((element) {
if (element.documentID.contains(currentUserId)) {
DocumentSnapshot document = element;
print(document);
}
});
});
It should works
you can work with something like this querying for where currentUser Id s equal to IdFrom or IdTo
StreamBuilder(
stream: Firestore.instance
.collection("users")
.orderBy('createAt', descending: true)
.where('idFrom', isEqualTo: currentuserId)
.where('idTo', isEqualTo: currentuserId)
.snapshots(),....
if you run this for the first time check your console for a link to create index

Get list of document I’d present inside a collection in firestore

I have a collection named requests and inside that collection I have many Document. I want to get the Id of each document present inside that collection
Firestore.instance
.collection(FirebaseCollection.user)
.where(UserCollectionField.mobile, isEqualTo: mobile)
.getDocuments()
.then((snapshot) {
var id = snapshot.documents[0].documentID;
}).catchError((error) {
});
also you can make list of user id ex.
Firestore.instance
.collection(FirebaseCollection.user)
.where(UserCollectionField.mobile, isEqualTo: mobile)
.getDocuments()
.then((querySnapshot) async {
var list = querySnapshot.documents;
list.forEach((document) {
adminlist.add(document.documentID);
});
});