skip to Main Content

We are developing an ecommerce application using Flutter/Dart and Firebase, and we are encountering some difficulties in managing multiple and dynamic filters.

Since Firebase doesn’t support the OR operator or searches using arrayContainsAny on multiple fields, we are uncertain about how to approach this functionality. Currently, we are generating the filters with pagination, meaning we take the first 20 results from the Firebase query and build the filters for the side panel using the data from those first 20 products. Consequently, many values are left out, as they don’t appear until we move to page 2, and the values from page 1 won’t appear either.

Since retrieving all the results from the query could lead to high consumption costs in Firebase, we are looking for an efficient approach.

What is recommended for managing this filtering functionality? Any guidance, examples, or insights into best practices would be greatly appreciated.

Thank you in advance!

2

Answers


  1. Option 1.

    Use a separate node to store all the possible filter options with the IDs of the items that correlate with the filter option this will cause a bit of data duplication but that’s fine.

    {
        "colors": {
            "red": ["<item-id-1>", "<item-id-2>", "<item-id-3>"],
            "blue": ["<item-id-4>"]
        },
        "sizes": {
            "small": ["<item-id-1>", "<item-id-2>"],
            "medium": ["<item-id-3>"],
            "large": ["<item-id-4>"]
        }
    }
    

    So if you want to filter on color: red, size: medium you take the first filter match and check it against the other filters option looking for a ID match. After this, you can query all the matches by ID.

    Option 2.

    Use a search service like Algolia or Elastic Search. Sadly they don’t have out-of-the-box support for Firebase.

    Option 3.

    Switch to Firestore(but you properly have your reasons for using Firebase).

    Login or Signup to reply.
  2. try to concatenate arrayContainsAny like this:

    coffeCollection.where("type", arrayContainsAny: ["arabica","robusta"]).where("country",arrayContainsAny: ["colimbia","mexico"]);
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search