Flutterexperts

Empowering Vision with FlutterExperts' Expertise
Implement searching with Firebase firestore | Flutter

Hi Everyone, In this article, I am going to show you how to search for a document in a collection.

What is the need?

Suppose we have a list of documents inside a collection and we want the search in the document list with respect to caseNumber

What do I have?

Query where(
String field, {
dynamic isEqualTo,
dynamic isLessThan,
dynamic isLessThanOrEqualTo,
dynamic isGreaterThan,
dynamic isGreaterThanOrEqualTo,
dynamic arrayContains,
bool isNull,
}){

}

Should I use isEqualTo?

List<DocumentSnapshot> documentList;
documentList = (await Firestore.instance
.collection("cases")
.document(await firestoreProvider.getUid())
.collection(caseCategory)
.where("caseNumber", isEqualTo: query)
.getDocuments())
.documents;

if you use isEqual then you will have to write the whole query, In my case, I will have to write the whole caseNumber to display the search result.

Or isGreaterThanOrEqualTo?

Flutter firestore plugin also allows searching for particular data in the document.

documentList = (await Firestore.instance
.collection("cases")
.document(await firestoreProvider.getUid())
.collection(caseCategory)
.where("caseNumber", isGreaterThanOrEqualTo: query)
.getDocuments())
.documents;

But you will not be satisfied with the result.

Source (Stack overflow)

What I really want?

Even if I type a single alphabet fo the search query I should get the most appropriate result from the firestore.

What should I do?

You will need to add the list of query options that a user will search for.

For example

For caseNumber (1011222) , user can search

[1 , 10 , 1011 , 10112 , 101122 , 1011222]

At the time when you are pushing data into the firestore you can do is to create the search query options.

{

"caseSearch": setSearchParam(caseNumber),

}

To add the list of query options

setSearchParam(String caseNumber) {
List<String> caseSearchList = List();
String temp = "";
for (int i = 0; i < caseNumber.length; i++) {
temp = temp + caseNumber[i];
caseSearchList.add(temp);
}
return caseSearchList;
}

This will result in pushing all the queries that a user can search for.

Implement

In the TextField, when text value is changing, it is quiring in the database

onChanged: (String query) {

getCasesDetailList(query);

}

Now we have the arrayContains in the query, all you need to do is check for the text value that is being typed and it firebase query will automatically search all the documents which have the caseSearch an array that contains the query.

List<DocumentSnapshot> documentList = (await Firestore.instance
.collection("cases")
.document(await firestoreProvider.getUid())
.collection(caseCategory)
.where("caseNumber", arrayContains: query)
.getDocuments())
.documents;

Now you will get all the list of documents that matches the search query.


Thanks for reading this article ❤

If I got something wrong, Let me know in the comments. I would love to improve.

Clap 👏 If this article helps you.

Connect with me on Linkedin and Github


Feel free to connect with us:
And read more articles from FlutterDevs.com.

FlutterDevs team of Flutter developers to build high-quality and functionally-rich apps. Hire a flutter developer for your cross-platform Flutter mobile app project on an hourly or full-time basis as per your requirement! You can connect with us on FacebookGitHubTwitter, and LinkedIn for any flutter-related queries.

We welcome feedback and hope that you share what you’re working on using #FlutterDevs. We truly enjoy seeing how you use Flutter to build beautiful, interactive web experiences.


Leave comment

Your email address will not be published. Required fields are marked with *.