Flutter: Search through text data from Firestore

6,873

Solution 1

The only type of text search that is currently possible on Cloud Firestore is a so-called prefix search. This allows findings strings that start with certain values, like Dark*, or strings starting with a certain range, like A* to B.

There is currently no way to search for values that contain a certain string, or more complex operations.

This applies to Firestore no matter how you access it. So it applies to Web, iOS, Android, Flutter, REST, and the Admin SDKs that Cloud Functions relies on.

The common way to add full-text search to an app built on Cloud Firestore it so integrate a third-party text search engine such as Algolia or ElasticSearch. For more on this, see the Firebase documentation.

Solution 2

Although too late.. I too encountered the problem and solved it using multiple where clauses with isGreaterThanOrEqualTo and isLessThan condition. Basically it searches for documents starting with the typed search term till search term appended with a 'z' character. Example to search for field values starting with 'foo' it will search for strings between 'foo' and 'fooz'.

currentStream = Firestore.instance
      .collection('collectionName')
      .where('searchKey', isGreaterThanOrEqualTo: type)
      .where('searchKey', isLessThan: type + 'z')
      .snapshots();
Share:
6,873
Darkhan
Author by

Darkhan

Updated on December 04, 2022

Comments

  • Darkhan
    Darkhan over 1 year

    I want to implement a search feature in my Flutter app. The text data is stored in Firestore.

    Now I'm considering an impractical solution: downloading the whole collection (which is a plain text) and then somehow search through it with Dart code.

    Is there a better way to do search in Flutter/Firestore? Can I use Cloud Functions to do that? And if there's no better way, how to search through content with Dart in Flutter?

  • Rana Hyder
    Rana Hyder almost 3 years
    This is the working solution right now! thanks.
  • Sayed Muhammad Idrees
    Sayed Muhammad Idrees over 2 years
    @frank-van-puffelen how does this works then? i just don't understand. you're saying contain string search won't work in firestore.