0

I made an application and coded a search system between documents in the application. But there is a problem:

enter image description here

enter image description here

The incoming record is registered as "Durmuş Bolat" in Firestore. And as you can see, the letter D of Durmuş is capitalized. That's why I have to capitalize the first letter in the search. And that's a problem.

I want to get the result of "Durmuş Bolat" even if I write it in the following ways:

  • durmuş bolat
  • DURMUŞ BOLAT
  • DuRmUs BoLaT

As a solution to this, I thought of shrinking the searched content and all the incoming content. So all incoming data and searched content will be downsized with toLowerCase.

I don't have the slightest idea where to place this toLowerCase code. Can you help me?

Codes:

var _searchText;

return Scaffold(
  appBar: AppBar(
    title: Container(
      height: 50,
      width: 250,
      child: TextFormField(
        autofocus: true,
        style: TextStyle(color: Colors.white),
        onChanged: (value) {
          setState(() {
            _searchText = value;
          });
        },
      ),
    ),
  ),
  body: Column(
    children: [
      Container(
        height: MediaQuery.of(context).size.height * 0.8, //MediaQuery.of(context).size.height * 0.8,
        child: StreamBuilder<QuerySnapshot<Map<String, dynamic>>>(
          stream: FirebaseFirestore.instance.collection('bolatTamir').where('nameSurname', isGreaterThanOrEqualTo: _searchText).snapshots(),
          builder: (context, snapshot) {
            if (!snapshot.hasData) {
              return Center(
                child: CircularProgressIndicator(),
              );
            }
            return ListView.builder(
              itemCount: snapshot.data!.docs.length,
              itemBuilder: (context, index) {
                return InkWell(
                  child: ListTile(
                    leading: CircleAvatar(
                      backgroundImage: NetworkImage(snapshot.data!.docs[index].data()['urunFotografi']),
                    ),
                    title: Text(snapshot.data!.docs[index].data()['nameSurname'], style: TextStyle(fontSize: 20),),
                    subtitle: Text(snapshot.data!.docs[index].data()['yapildiMi'] == false ? 'Tamamlanmadı' : 'Tamamlandı', style: TextStyle(fontSize: 17),),
                    trailing: Text(snapshot.data!.docs[index].data()['tamirUcreti'], style: TextStyle(fontSize: 20),),
                  ),
                );
              },
            );
          },
        ),
      ),
    ],
  ),
);

Thank you in advance for your help.

Emir Bolat
  • 899
  • 3
  • 14
  • 37
  • 1
    Just using `toLowercase` isn't enough, as not all characters map correctly when doing that. You'l want a more complex mapping, based on a [collation sequence](https://www.ibm.com/docs/en/i/7.4?topic=concepts-collating-sequence). Also see Dan's excellent answer here: https://stackoverflow.com/questions/48096063/cloud-firestore-case-insensitive-sorting-using-query – Frank van Puffelen May 20 '22 at 16:23
  • @Frank van Puffelen I still want to minimize the incoming results and what I'm looking for and search for a result. How can I do that? Can you post it as an answer? – Emir Bolat May 20 '22 at 22:03

1 Answers1

0

To my knowledge, lower-casing text that is already stored is not possible with Firebase. One can only lower-case the search term.

If it is just names and not tons of text, one could

  • .split(' ') these names, then
  • .toLowerCase() all resulting words, then
  • store them in a search-index-collection.

Then, search this search-index-collection and resolve the $userId. .

users/34td24y3gtdb724/
{
   name: 'Durmuş Bolat'
}

searchindex/
{
  word: 'durmuş',
  userId: 34td24y3gtdb724

  word: 'bolat',
  userId: 34td24y3gtdb724
}


Google itself asks to use Third-Party-Providers for full text search:

https://cloud.google.com/firestore/docs/solutions/search

Also, there is an approach to generate permutations as an Array:

https://medium.com/flobiz-blog/full-text-search-with-firestore-on-android-622af6ca5410

Dabbel
  • 2,468
  • 1
  • 8
  • 25