Home > Enterprise >  Is there any way to retrieve multiple documents by ID using FireStore (FlutterFire)?
Is there any way to retrieve multiple documents by ID using FireStore (FlutterFire)?

Time:10-10

In my flutter/dart app, I am using FlutterFire to sync data with my FireStore database.

I have a list of IDs from document (of the same collection) that I want to retrieve from FireStore.

I am trying to do the following:

await FirebaseFirestore.instance.collection('groups').doc([id1, id2, id3, id4]).get();

But ".doc()" won't accept a list as a parameter for the document ID to look for.

I tried with the "where" method like this:

await FirebaseFirestore.instance.collection('groups').where("id", whereIn: [id1, id2, id3, id4]).get();

But it won't work since the "id" isn't a field in the documents (it is their name/identifier).

The only two solutions I can think about are:

Doing one request per document I want to retrieve (not ideal at all) Adding a field "id" in the documents that store the same information as their name/identifier (I don't like duplicating data that can end mismatching) The third one (ideal IMO) would be to enable .doc to accept lists/arrays, so you can search for a list of documents you know the identifier of.

Am I missing something? Does anybody know any other solution?

THANKS!

(asked the same question here)

CodePudding user response:

I found a solution here!

So adapting it to my case it looks like this:

await FirebaseFirestore.instance.collection('groups').where(FieldPath.documentId, whereIn: [id1, id2, id3, id4]).get();

Be aware that there is a limit of 10 to the number of values/IDs you can use in the search.

  • Related