Jagadish
Jagadish

Reputation: 1186

Flutter: Auto Complete search bar with data from 3 different Firestore collection

I want to have an auto complete search app bar, in which data is from 3 collections which have 2 fields common in each doc of them. I want to get all docs from 3 collection and show the 2 fields as title and subtitle of the suggestion listtile. How to get suggestions data from 3, collections?

Upvotes: 0

Views: 381

Answers (2)

Jagadish
Jagadish

Reputation: 1186

I couldn't find a way to get data from 3 collections separately at a time, so did it another way I don't know if it's the most efficient one. Still, it might help you out.

Let you have three collections A, B, C so what I did was I created another common collection i.e X

so when you use the fucntion addDataToA()or addDataToB() or addDataToC() , also include X in them for example: addDataToA( addDataToX(); ) similarly for B and C, Now as you have done for adding data you can also do it for the update, create and delete.

For more convenience what I did was in docs of collection A, B, C I added a field called type in which I wrote A, B, C so that while retrieving data through the collection X you can also know from which collection it came.

Hope this helps, any doubts please ask in the commenst bellow.

Upvotes: 1

Frank van Puffelen
Frank van Puffelen

Reputation: 599591

If the 3 collections all have the same name, consider using a collection group query to search them.

If they have different names, or if there are many more collections of the same name that you don't want to search, you will have to query each of the 3 collections individually and merge the results in your application code.

Upvotes: 0

Related Questions