How to search text in Flutter using Firestore

6,169

Solution 1

Firestore does not have built-in full-text-search capabilities. The only two query operators I commonly use for searching text fields are:

  • Using isEqualTo: to find documents where the field matches the value exactly.

  • Using isGreaterThanOrEqualTo: and isLessThanOrEqualTo: (or startAt: and endAt:) to search for documents where the field starts with a specific value.

    For example, to search for all documents where name starts with al, you'd use collectionRef.where("name", isGreaterThanOrEqualTo: "al").where("name", isLessThanOrEqualTo: "al\uf7ff"). The \uf7ff here is just the last known Unicode character, so that the query stops returning results after dealing with every al.

If you want any other text search operations, you'll need to use an additional solution for that - such as Algolia that is documented here.

Solution 2

this code will be very super helpful in searching text by using firestone flutter

 StreamBuilder(
    stream: ( searchtxt!= "" && searchtxt!= null)?FirebaseFirestore.instance.collection("addjop").where("specilization",isNotEqualTo:searchtxt).orderBy("specilization").startAt([searchtxt,])
        .endAt([searchtxt+'\uf8ff',])
        .snapshots()
        :FirebaseFirestore.instance.collection("addjop").snapshots(),
    builder:(BuildContext context,snapshot) {
      if (snapshot.connectionState == ConnectionState.waiting &&
          snapshot.hasData != true) {
        return Center(
            child:CircularProgressIndicator(),
        );
      }
      else
        {retun widget();

} } )

Share:
6,169
Alpit Panchal
Author by

Alpit Panchal

Updated on December 30, 2022

Comments

  • Alpit Panchal
    Alpit Panchal over 1 year

    I want to implement search functionality using cloud Firestore by a query but not getting any query related to fetch data. I have searched many things on google but everywhere the only suggestion is first to get all data from cloud Firestore then searches locally. I have checked the where condition inside of the query but not finding any condition like "start with" etc.

    enter image description here

    I want to search by the name parameter.

    enter image description here

    • Victor Eronmosele
      Victor Eronmosele almost 3 years
      Do you want to search for documents where the name field exactly matches the search term or do you want to a full text search (firebase.google.com/docs/firestore/solutions/search) ?
    • Alpit Panchal
      Alpit Panchal almost 3 years
      Thanks for response suppose I have many records so if I have searched the "al" so all text will show who starts with and matches with "al" text.
    • Victor Eronmosele
      Victor Eronmosele almost 3 years
      Okay, that's a full-text search. Please visit the link in my first comment to see how it is implemented. You will need a third-party search service like Algolia.
    • Alpit Panchal
      Alpit Panchal almost 3 years
      I have checked the URL as you suggest but can't understand this if you have any demo related to this so please share with me. And I have also searched the (pub.dev/packages/firestore_search) plugin so can you please suggest is use full or not.
    • Victor Eronmosele
      Victor Eronmosele almost 3 years
      Checkout dev.to/samarthagarwal/… for the demo.