0

I am fetching products from firestore below is my schema,

- products (collection)
  |
  |- [Product_ID_1] (document)
  |  |- name: "XYZ"
  |  |- department: "ABDG"
  |  |- genericNames: ["OP", "NY"]
  |  |- price: 55
    |
  |- [Product_ID_2] (document)
  |  |- ...

So, User do search on the screen and once we get search query we have to fetch products from products collection on the basis of either matching name or department or generic names.

I want to achieve something like MySQL like keyword do along with where or condition.

below is my code but not working as expected.

Future<void> searchProducts({String query = "Achiote"}) async {
    Future.delayed(Duration.zero);

    List<Map<String, dynamic>> searchResults = [];

    try {
      state = AsyncValue.loading();

      QuerySnapshot<Map<String, dynamic>> snapshot = await _fireStore
          .collection('products')
          .where('genericNames', arrayContains: term)
           .where('department', isEqualTo: term)
           .where('name', isEqualTo: term)
          .get();

      // Add the search results to the list
      searchResults = snapshot.docs.map((doc) => doc.data()).toList();
      state = AsyncValue.data(searchResults);
    } catch (e) {
      print('Error searching products: $e');
      state = AsyncValue.error('Error searching products: $e', StackTrace.current);
    }
  }

So, User do search on the screen and once we get search query we have to fetch products from products collection on the basis of either matching name or department or generic names.

I want to achieve something like MySQL like keyword do along with where or condition.

  • "something like MySQL like keyword" Firestore doesn't support full-text search operations. Did you see https://stackoverflow.com/questions/46568142/google-firestore-query-on-substring-of-a-property-value-text-search? – Frank van Puffelen Jul 21 '23 at 19:43
  • Maybe this [resource](https://medium.com/firebase-tips-tricks/how-to-filter-firestore-data-cheaper-705f5efec444) will also help. – Alex Mamo Jul 22 '23 at 10:44
  • Thanks, I will look into this. – Rahul Gohil Jul 26 '23 at 18:12

0 Answers0