I'm working on an eCommerce app and working on a coupon system while creating a query I'm getting this error every time.
FirebaseException ([cloud_firestore/failed-precondition] Operation was rejected because the system is not in a state required for the operation's execution. If performing a query, ensure it has been indexed via the Firebase console.)
I tried to search the error but couldn't find the exact solution as some answers suggest that I have to create a collection index which I did but still getting the same error.
This how my query looks like:-
Future<void> fetchCouponCode(
String couponCode,
String userId,
String priductCategory,
String productIds,
) async {
if (couponCode == null) {
return;
}
if (userId == null) {
return;
}
print(couponCode);
var today = DateTime.now();
await FirebaseFirestore.instance
.collection('coupons')
.where('code', isEqualTo: couponCode)
.where('product_categories', isEqualTo: priductCategory)
.where('customer_email', isEqualTo: 'all')
.where('product_ids', arrayContainsAny: [productIds])
// .where('constraints.BagConstraints.min_item', isGreaterThan: 1)
.where('is_enabled', isEqualTo: true)
.where('valid_from',
isGreaterThanOrEqualTo: today)
.get()
.then((QuerySnapshot querySnapshot) {
print('=========object=======');
querySnapshot.docs.forEach((element) {
print(element.get('discount'));
});
});
}
This is how my coupon structure looks like
var uuid = Uuid();
{
'coupon_id': uuid.v4(),
'code': 'V710OFF',
'description': 'Summer Sale',
'discount': 40,
'minimum_dicount': 30,
'maximum_discount': 150,
'discount_type': 'percent',
'product_ids': ['3'],
'date_created': DateTime.now(),
'exclude_product_ids': 2,
'product_categories': 't-shirts',
'exclude_product_categories': '',
'customer_email': 'all',
'constraints': {
'commonConstraints': {
'uniqueId': uuid.v4(),
'device_type': 'ios',
'valid_from': DateTime.now(),
'valid_until': DateTime.now().add(Duration(days: 10)),
'usage_limit': 1,
'usage_limit_per_user': 1,
'total_usage': 10
},
'BagConstraints': {
'uniqueId': uuid.v4(),
'min_value': 120,
'max_value': 3000,
'min_item': 1,
'max_item': 4,
}
},
'is_enabled': true
}
I need to check if the coupon is invalid date range. So shall I do inside querySnapshot.docs.forEach as this can be done but what if I have a similar issue with another collection. How do I query third-level docs or this can't be achieved right now?? Also I go through firestore documentation and youtube channel but couldn't find the solution
And this is how I query coupon
fetchCouponCode(
'V710OFF', currentFirebaseUser.uid, 't-shirts', '3');


