Flutter/Firebase get documents by list of IDs using .get() - firebase

This is a followup to Flutter Firebase get documents by array of IDs.
How would I get a Query of documents from a collection using a list of IDs? Let's says I have the following:
List<String> someList = ['abc123', 'def456', 'hij789']; // Need to query for these documentIDs
I'm using FieldPath.documentId() which works, but limits the query to 10.
Query query = Firestore.instance
.collection(APIPath.products())
.where(FieldPath.documentId(), whereIn: someList);
I would normally pass query to a function of mine that would return a Stream<List<Product>> as seen below.
return _service.queryStream(
query: query,
path: APIPath.products(),
builder: (data) => Product.fromMap(data),
);
I know get() is a better option, but not sure what the code would look like if I wanted to return a Stream<List<Product>>.

Related

how we can run multiple queries on Firebase/firestore to get filtered data in flutter?

how we can run multiple queries on Firebase/ firestore to get filtered data in flutter or any other possible solution? there is a picture storing dummy data in goal and title and wanna users according to the goals and titles filters.
Where() is a function provided by Firebase to query data
FirebaseFirestore.instance.collection('yourCollectionName').where('field1', isEqual:'value1').where('field2', isEqual: 'value2').get();
you can read more about it in the querying section at https://firebase.flutter.dev/docs/firestore/usage/
By looking at your Firestore database it is clear that you want to filter the documents based on two array fields goals and titles.
Firestore supports the arrayContains filter in the where clause by which you can filter data from an array inside Firestore. So you may think of doing something like this -
var filter1 = 'Hire Employees';
var filter2 = 'CEO';
FirebaseFirestore.instance
.collection('collectionId')
.where('goals', arrayContains: filter1)
.where('titles', arrayContains: filter2)
.get()
.then((QuerySnapshot docs) => {
for (var element in docs.docs) {print(element.data())}
});
But the above will throw an error saying that You cannot use 'array-contains' filters more than once. This is because Firestore has a limitation that you can use at most one array-contains clause per query which is documented here. So it is not possible to do something like the above.
I am not very sure of your use case, but as a workaround, you can restructure the database structure by making any one of the array field to a map, something like the following -
By making the structure like the above you can use multiple where clauses with one arrayContains filter and one isEqualTo filter which will look something like this -
var filter1 = 'Hire Employees';
var filter2 = 'CEO';
FirebaseFirestore.instance
.collection('collectionId')
.where('goals.$filter1', isEqualTo: true)
.where('titles', arrayContains: filter2)
.get()
.then((QuerySnapshot docs) => {
for (var element in docs.docs) {print(element.data())}
});
As the idea of allowing multiple arrayContains filters is currently not supported, but could be valid, I would recommend you file a feature request.

Flutter Firebase How to get random document

I am trying to get some random posts from Firebase. But i am unable to get random document id.
Is there any way to retrieve data from Firebase like this :-
getRandomData() async {
QuerySnapshot snapshot = await posts
.document(random.id)
.collection('userPosts')
.getDocuments();}
i am trying to say that. Now i am able to get documentID normally not in italics.so now how can i get random documentID from Firebase.
List documentIds in a list first.
var list = ['documentId1','documentId2','documentId3'];
var element = getRandomElement(list);
Then query the documentSnapshot
You can first get all documents in you collection.
Try this code:
async getMarker() {
const snapshot = await firebase.firestore().collection('userPosts').get();
const documents = [];
snapshot.forEach(doc => {
documents[doc.id] = doc.data();
});
return documents;
}
Next, from return documents you can create a list of documents id and get random numbers (document id) from this list.
The main problem here that's going to prevent you from moving forward is the fact that you don't actually have any documents nested immediately under "posts". Notice that the names of the documents are in italics. That means there isn't actually a document here at all. The reason why they are show, however, is because you have a subcollection "userPosts" nested under the path where that document ID exists.
Since you don't have any documents at all under "posts", the usual strategies to find a random document won't work at all. You're going to have to actually populate some data there to select from, or find another way to select from the data in the subcollections.
firestore()
.collection('SOURCE')
.doc(props?.route?.params?.data?.id)
.collection('userPosts')
.get()
.then(querySnapshot => {
querySnapshot.forEach(documentSnapshot => {
console.log('User ID: ', documentSnapshot.id, documentSnapshot.data());
})
})

Unable to get the documents after creating a query index

Code:
var querySnapshot = await Firestore //
.instance
.collection('collection')
.where('name', isEqualTo: ['foo'])
.orderBy('time')
.limit(1)
.getDocuments();
print('${querySnapshot.documents}'); // prints []
It returns empty List.
Database structure:
Index built
Indexing isn't an issue here. Given the query and document you're showing, I'd always expect it to return no results. You're using an array-contains type query on a field that isn't an array. Your name field is a string, and strings can't be matched by array-contains queries.
If you intended for name to be an array, you'll need to modify the document so that it is actually an array with the string "foo" in it.

Flutter Firebase get documents by array of IDs

How would I get a Query of documents from a collection using a list of IDs? Let's says I have the following:
List<String> someList = ['abc123', 'def456', 'hij789']; // Need to query for these documentIDs
I would normally do the following. But this obviously won't work since I need to query the documentIDs.
Query query = Firestore.instance
.collection(APIPath.products())
.where('someField', whereIn: someList);
Try using FieldPath.documentId().
Query query = Firestore.instance
.collection(APIPath.products())
.where(FieldPath.documentId(), whereIn: someList);
Note that you are limited to 10 items in the list, and this might actually be slower than just requesting each document individually with get().

Flutter Cloud Firestore whereIn clause

I try to get some lists from cloud firestore with this code
final User user = UserUtils.currentUser(context);
final List<String> memberLists = user.memberLists.toList();
final List<String> ownedLists = user.ownedLists.toList();
final List<String> visibleListsIds = memberLists + ownedLists;
final Query query = (sharedCollection.where(FIELD_LIST_ID, whereIn: visibleListsIds));
log.d("queryPath: ${query.reference().path}");
log.d("buildArguments: ${query.buildArguments()}");
return query.snapshots().map((QuerySnapshot querySnapshot) {
return querySnapshot.documents
.map(
(document) {
log.d("document: ${document.documentID}");
return _getSerializers()
.deserializeWith(WishList.serializer, document.data);
},
)
.where((value) => value != null)
.toList();
});
When I do that I do not get any logs with "document: ..." because obviously the query does not get the documents. Here are the buildArguments of the query:
buildArguments: {where: [[listId, in, [IIAY56KF0mSCx1WwCXaV]]], orderBy: [], path: WISHLISTS}
Note: Wishlists is the correct path and the listId is also correct.
If I change the query line to: final Query query = sharedCollection; it just spits out all lists, so it should work in theory. I think there is some issue with the whereIn operator. Does anyone have a clue why this is not working? I am happy for any help.
The whereIn operator uses the same query documented here, and is limited to 10 equality clauses. A workaround here is to split the List visibleListsIds to smaller chunks, similar to what have been mentioned on this Stack Overflow post.

Resources