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.