Need to check if a field value exists inside a Firestore document? Here’s the code for it!

Share This Post

I had a use case where I wanted to validate a code entered against a field inside a Firestore collection document. My goal was to get a true/false response. It’s easy to check if the document exists, but I struggled when trying to find if the value exists inside a specific field…

Enter Custom Actions…

Here’s the custom action code I used, then I just run the custom action with the textinput provided and get a boolean response.

				
					Future<bool> checkIfCodeExists (String CODETOCHECK) async {

  QuerySnapshot querySnapshot = await FirebaseFirestore.instance
      .collection('COLLECTION_NAME')
      .where('CODE FIELD NAME', isEqualTo: CODETOCHECK)
      .limit(1)
      .get();

  return querySnapshot.docs.isEmpty;
}
				
			

More To Explore

DocRaptor – a lifesaver for PDF generation

Great software deserves a shoutout for being great… I’ve been using DocRaptor to turn HTML into PDFs for a few projects, and it’s ridiculously simple

Let's Talk

You can also email me at hello@depalma.co