Search code examples
flutterfirebasedartgoogle-cloud-platformgoogle-cloud-firestore

Flutter firebase search query without case sensitivity


I have a search bar where I can search data from Firestore. But with this it is case-sensitive. How can I change it so it's case-insensitive?

var name = "";
final data = snapshot.data!.docs[index].data();

if (data["name].toString.startWith(name.toLowerCase().trim())){
                 return ....
                } //With this I can search data which ONLY start with lowerCase

if (data["name].toString.startWith(name.trim())){
                 return ....
                } //With this I can search data that starts with any Case but according to the Case I input. That is, if I start with upperCase, only data starting with upperCase appear and vice versa

I also tried removing the .trim() in both instances but the same result.

What can I do do the search query is insensitive to cases?


Solution

  • All queries in Cloud Firestore are case-sensitive. If you need a case-insensitive mechanism, then you'll need to create a separate field that contains the case-insensitive version of the field and query against it.

    There is also a great answer from @DanMcGrath that I recommend you check:

    Or an alternative answer from @samthecodingman:

    One last thing to mention is that the trim() function returns a string, with the leading and trailing whitespace omitted. It has nothing to do with case-sensitive or case-insensitive.