user13002252
user13002252

Reputation:

How to check if a document with a field having some value exists and return true/false?

I got a Firebase Firestore database. And I want to check if in my database exists a document that contains a field with some data. And if exists return true otherwise false.

I'm using this in an onPressed function so thats why I need a value of true of false, because if it's true the other tasks can continue, but if it's not I want to display an error message. It's an invitation code that some particular users have, and I want to check if some user has that code, it means the invitation code it's correct.

I've tried

var users = FirebaseFirestore.instance
    .collection('users')
    .where('invitation_code', isEqualTo: inputCode)
    .get();

but I cant use " users.length " to see if the length is greater than 0 or not.

I'm using Flutter.

Can you please help me?

Thank you!

Upvotes: 1

Views: 1971

Answers (1)

Raine Dale Holgado
Raine Dale Holgado

Reputation: 3460

You can check its document if empty or not

FirebaseFirestore.instance
        .collection('users')
        .where('invitation_code', isEqualTo: inputCode)
        .get()
        .then((value) {
      if (value.docs.isNotEmpty) {
        // return true;
      } else {
        // return false;
      }
    });

Upvotes: 3

Related Questions