How to search through multiple fields in my firestore database through flutter

1,023

You can't pass multiple conditions to where(). Instead you can call where() multiple times, chaining them together into the query you need:

usersReference
  .where("profileName",isGreaterThanOrEqualTo: searchWord)
  .where("username",isEqualTo: "QeustionableCoder")
  .getDocuments();

Note that you may need to define an index for such multi-field queries. If you're missing an index, the SDK will log an error message about that. Look for the URL in that error message, as it is the fastest way to define an index on the correct fields.

Share:
1,023
Sylvester Agyapong
Author by

Sylvester Agyapong

Updated on December 21, 2022

Comments

  • Sylvester Agyapong
    Sylvester Agyapong over 1 year

    I'm trying to build a search functionality where a text entered in my search field is search through two fields in my Firestore(profileName and username). For example, if a user has the profileName 'Sylvester Appiah' and username 'keeett'. Then when either the username or profileName is searched it should return the same user. I'm not sure if I can do profileName or username inside the .where() method. Any help is appreciated!

    controlSearch(String searchWord){
     Future<QuerySnapshot>pUsers=usersReference 
      .where("profileName",isGreaterThanOrEqualTo:searchWord)          
      .getDocuments();
       setState(() {
          futureSearchResult = pUsers;
    
        });
      }