How to loop through a DocumentSnapshot.data and retrieve id's and keys? - flutter

How do I loop through a DocumentSnapshot.data() object and retrieve its keys and values?
My code currently looks like this:
Future<void> drawPolygons() async {
var points = await FirebaseFirestore.instance
.collection('polygons')
.doc(uid)
.get()
.then((DocumentSnapshot documentSnapshot) {
if (documentSnapshot.exists) {
//if user has polygon data on account
var data = documentSnapshot.data();
}
});
}

**Update Your Method and Convert the data to Map**
Future<void> drawPolygons() async {
var points = await FirebaseFirestore.instance
.collection('polygons')
.doc(uid)
.get()
.then((DocumentSnapshot documentSnapshot) {
if (documentSnapshot.exists) {
Map data = (documentSnapshot.data() as Map);
for (var entry in data.values) {
print(entry);
}
}
});
}

Related

How to fetch a `DocumentReference` from a Firebase `get()`

I have a collection ads that contains a DocumentReference as ownerId.
With the code below, I am able to fetch the 10 most recent ads as aList<Ad>:
/// Returns a list of ads of the given [category]
static Future<List<ClassifiedAd>> getFromCategory(
ClassifiedAdCategory category,
{int max = 10}) async {
return FirebaseFirestore.instance
.collection('ads')
.where('category', isEqualTo: category.name)
.orderBy('creationDate', descending: true)
.limit(max)
.get()
.then((snapshot) {
return snapshot.docs.map((doc) {
final data = doc.data();
return Ad.fromMap(data);
}).toList();
});
But now I'd like to fetch the owner (collection users) from the DocumentReference I was talking about above. But I am a but puzzled about how to do that.
My modified code below does not compile:
The return type 'List' isn't a 'FutureOr<List>', as required by the closure's context.
/// Returns a list of ads of the given [category]
static Future<List<ClassifiedAd>> getFromCategory(
ClassifiedAdCategory category,
{int max = 10}) async {
return FirebaseFirestore.instance
.collection('ads')
.where('category', isEqualTo: category.name)
.orderBy('creationDate', descending: true)
.limit(max)
.get()
.then((snapshot) {
// <<<< Error starts right here down to the removeWhere()
return snapshot.docs.map((doc) {
final data = doc.data();
final DocumentReference docRef = data["ownerId"];
return docRef.get().<ClassifiedAd?>then((snapshot) {
if (snapshot.exists) {
return ClassifiedAd.fromMap(data);
}
return null;
});
}).toList()
// Don't take into account potential nulls
..removeWhere((a) => a == null);
});
How should I do that?
I would say that the wrong thing that you're doing is you're trying to get a snapshot asynchronously inside the map() method which is synchronous, for such cases like yours, I recommend using await/async and to not return anything until you guarantee that you got it, try this:
static Future<List<ClassifiedAd>> getFromCategory(
ClassifiedAdCategory category,
{int max = 10}) async {
final snapshot = await FirebaseFirestore.instance
.collection('ads')
.where('category', isEqualTo: category.name)
.orderBy('creationDate', descending: true)
.limit(max)
.get();
List<ClassifiedAd> result = [];
for (int index = 0; index < snapshot.docs.length; index++) {
final doc = snapshot.docs[index];
final data = doc.data();
final DocumentReference docRef = data["ownerId"];
final docOwnerSnapshot = await docRef.get();
if (docOwnerSnapshot.exists) {
result.add(ClassifiedAd.fromMap(data));
}
}
return result;
}

Toggling a favorites button while adding and deleting items from firestore

I want to toggle a favourites button using firestore. When i click on it initially, I want to add it to a collection, then clicking again should remove that exact item from the collection. My issue here is that, I could achieve this by setting the doc name to the title and simply delete using the title has reference. This would have an issue later on, since two products can technically have the same name. That is why I want to use the id has a reference name since it is dynamic. This is the code below.
try {
if (widget.product['isFavourited'] == true) {
String docId = widget.product.id;
// print(docId.toString());
// print(widget.product['isFavourited'].toString());
await FirebaseFirestore.instance
.collection('Products')
.doc(docId)
.update({'isFavourited': false}).then((value) async {
final CollectionReference collectionReference =
FirebaseFirestore.instance
.collection('Users')
.doc(uid)
.collection('Favourites');
await collectionReference
.doc()
.delete();
print('');
});
} else {
String docId = widget.product.id;
await FirebaseFirestore.instance
.collection('Products')
.doc(docId)
.update({'isFavourited': true}).then((value) async {
final CollectionReference collectionReference =
FirebaseFirestore.instance
.collection('Users')
.doc(uid)
.collection('Favourites');
final String id = collectionReference.doc().id;
final String itemtodelete = collectionReference.doc(id).id;
print(itemtodelete);
print(id);
await collectionReference.doc(itemtodelete).set({
'id': itemtodelete,
'title': widget.product['title'],
'price': widget.product['price'],
'about': widget.product['about'],
'description': widget.product['description'],
'imagepath': widget.product['imagepath'],
'isFavourited': widget.product['isFavourited'],
'isCarted': widget.product['isCarted'],
});
});
try is out hope this will help.
// productId is your productId = widget.product.id
// uid is your current user id
// favorite id is your each favorite documentId
Future<void> addToFavorite(String productId, String uid, favoriteId) async {
final productCollection = FirebaseFirestore.instance.collection("Products");
final favoriteCollection = FirebaseFirestore.instance.collection("Users").doc(uid).collection("Favorites");
final productDocReference = await productCollection.doc(productId).get();
if (productDocReference.exists) {
final favoriteDocReference = favoriteCollection.doc(favoriteId).get().then((favoriteDoc) {
if (!favoriteDoc.exists) {
favoriteCollection.doc(favoriteId).set({
'id': favoriteId,
'title': widget.product['title'],
'price': widget.product['price'],
'about': widget.product['about'],
'description': widget.product['description'],
'imagepath': widget.product['imagepath'],
'isFavourited': widget.product['isFavourited'],
'isCarted': widget.product['isCarted'],}).then((value){
productCollection.doc(productId).update({
"isFavourited": true
});
});
} else {
favoriteCollection.doc(favoriteId).delete().then((value) {
productCollection.doc(productId).update({
"isFavourited": false
});
});
}
});
}
}
This code worked perfectly for me, given the way I structured my function. #Adnan Khan's answer was really helpful in figuring it out. I hope this helps someone who is stuck.
Future<String> addtoFavourites(QueryDocumentSnapshot data, String uid) async {
final CollectionReference collectionReference = FirebaseFirestore.instance
.collection('Users')
.doc(uid)
.collection('Favourites');
final String id = collectionReference.doc().id;
String docId = data.id;
try {
if (data['isFavourited'] == true) {
await FirebaseFirestore.instance
.collection('Products')
.doc(docId)
.update({'isFavourited': false}).then((value) async {
print(docId);
await collectionReference.doc(docId).delete();
return 'Removed from favourites';
});
} else {
await FirebaseFirestore.instance
.collection('Products')
.doc(docId)
.update({'isFavourited': true}).then((value) async {
print(docId);
print(id);
await collectionReference.doc(docId).set({
'id': docId,
'title': data['title'],
'price': data['price'],
'about': data['about'],
'description': data['description'],
'imagepath': data['imagepath'],
'isFavourited': true,
'isCarted': data['isCarted'],
});
});
}
notifyListeners();
return 'Added to favourites';
} catch (e) {
return e.toString();
}

async function not completing when querying FirebaseFirestore

See the print statement down below. It never executes.
Future<void> populate() async {
final userId = FirebaseAuth.instance.currentUser!.uid;
final db = FirebaseFirestore.instance;
// Get list of ids of parties use swiped on.
var snapshot1 = await db
.collection("partiers_swipes")
.where('userId', isEqualTo: userId)
.get();
var partyIdsUserSwipesOn = [];
if (snapshot1.size > 0) {
snapshot1.docs.forEach((element) {
partyIdsUserSwipesOn.add(element.data()['partyId']);
});
}
var snapshot2 = await db
.collection("parties")
.where(FieldPath.documentId, whereNotIn: partyIdsUserSwipesOn)
.get();
print('This never executes');
}
The whereNotIn argument is not supported by the where clause. This crashes the function.

Access firestore value

This is firestore database structure. I'm trying to call these data in my flutter application. but I only get value upto document which is chatroom/9000. But i want collection of document 9000. How do i call it?
This is how i called query
await _firestore .collection('chatroom') .doc(UUID) .get() .then((value) { });
Future<dynamic> getValues() async{
var _chatroom =await _firestore .collection('chatroom').get((value)async{
if(value !=null ){
for(var index = 0 ; index< value.documents.length ; index++ )
var _chatitem = await _firestore .collection('chatroom')
.doc(value.value.documents[index].documentId) .get() .then((value) {
// Do what you Want
})
}
});
}

wait for firestore documents in for loop

trying to fetch results within a for loop , but for loop doesn't wait for firestore results.tried forEach as well before .
Future<bool> checkIfNewMessages() async{
bool hasNewMessage=false;
QuerySnapshot _myDoc = await Firestore.instance.collection('PropMap')
.orderBy('ProJoiningDate')
.where('TenId', isEqualTo: globals.memberAuthId)
.getDocuments();
List<DocumentSnapshot> properties = _myDoc.documents;
if(properties.length>0)
for(final property in properties) {
//properties.forEach((property) { //tried forEach() as well
String propid= property.data['PropertyId'];
if(property.data['LastVisitTime']!=null) {
DateTime tenantsLastPropVisitTime = property.data['LastVisitTime'].toDate();
getLastPropertyChatTime(propid).then((latestPropChatTime) { //This 'then' seems not working
print('LAST chat date is ${latestPropChatTime}');
if (latestPropChatTime.isAfter(tenantsLastPropVisitTime)) //This means he has not seen new messages , lets notify him
{
hasNewMessage= true;
}
});
}
};
return hasNewMessage;
}
And these are the fetch methods,when the breakpoint is at getDocuments() of getTheLastChat() the control just jumps back to for loop again without waiting for results .
Future getTheLastChat(propId) async {
QuerySnapshot _myDoc =await Firestore.instance.collection('Chats').orderBy('ChatDate', descending: true)
.where('PropertyId', isEqualTo: propId)
.limit(1)
.getDocuments();
List<DocumentSnapshot> tenants = _myDoc.documents;
return tenants;
}
Future<DateTime> getLastPropertyChatTime(propId) async {
DateTime lastChatTime= DateTime.now().add(Duration(days:-365));
var lastChatTimeDocs = await getTheLastChat(propId);
lastChatTime=lastChatTimeDocs.length>0?lastChatTimeDocs[0].data["ChatDate"].toDate():DateTime.now().add(Duration(days:-365));
return lastChatTime;
}
You can use the Future.forEach to achieve your requirement
Future<void> buildData(AsyncSnapshot snapshot) async {
await Future.forEach(snapshot.data.documents, (element) {
employees.add(Employee.fromSnapshot(element));
});
}