How to search text in Flutter using Firestore
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:
andisLessThanOrEqualTo:
(orstartAt:
andendAt:
) to search for documents where the field starts with a specific value.For example, to search for all documents where
name
starts withal
, you'd usecollectionRef.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 everyal
.
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();
} } )
Alpit Panchal
Updated on December 30, 2022Comments
-
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.
I want to search by the name parameter.
-
Victor Eronmosele almost 3 yearsDo 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 almost 3 yearsThanks 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 almost 3 yearsOkay, 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 almost 3 yearsI 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 almost 3 yearsCheckout dev.to/samarthagarwal/… for the demo.
-