0

I am using firestore in flutter. I have to create a group like whatsapp group for that I'm getting group name, group currency,group admin and a list of members which includes fullname and phonenumber as you can see

groupadmin : "+923009399237"
groupcurrency : "USD"
groupmembers : "[
            {"fullname":"ABC",
             "phonenumber":"+923475894561"
            },
            {"fullname":"ASD",
             "phonenumber":"+922222222211"
            }
            ]"
groupname : "First Group"

I have tried something like this but it doesn't work.

 Firestore.instance
    .collection('groups')
    .where("groupmember.phonenumber", isEqualTo: 090909090)
    .snapshots()
    .listen((data) {
  if (data.documents.length > 0) {
 }

Is there anyway to directly find the group which has member with phonenumber == 0909090909. Please help me out in this.

Constantin Beer
  • 5,447
  • 6
  • 25
  • 43
H.J
  • 1
  • 1

2 Answers2

1

Worth noting that you can also add exists to your Firestore rules as well. This way you can call a Firebase Function that relies on data existing, and then safely handle success/errors by placing things within a try/catch. Note that exists counts as a Firestore read (for billing purposes).

e.g. allow create: if exists(/databases/$(database)/documents/users/$(request.auth.uid))

See: https://firebase.google.com/docs/firestore/security/rules-conditions

Ryan Unger
  • 19
  • 6
0

Use property exists:

 Firestore.instance
    .collection('groups')
    .where("groupmember.phonenumber", isEqualTo: 090909090)
    .snapshots()
    .listen((data) {
  if (data.exists) console.log("Yeah !");
 }

See also: What's the best way to check if a Firestore record exists if its path is known?

And please read the doc before posting https://firebase.google.com/docs/firestore

Emeric
  • 6,315
  • 2
  • 41
  • 54
  • Can you please refer to some flutter example? – H.J Sep 11 '19 at 11:42
  • Maybe this topic might help you https://stackoverflow.com/questions/51122211/check-if-field-already-exists-in-flutter-firestore – Emeric Sep 11 '19 at 11:46