Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to check if Field exist in document in firestore database?

My Firestore Database :

Firestore database screenshot

i want to check if there is a field named "[email protected]" exist in Doc "D9GeGdTxarFruuuQklEO" only ,, not checking in all docs ,, anyway to do that in firestore Flutter ?

like image 227
Saifallak Avatar asked Sep 15 '18 02:09

Saifallak


1 Answers

From cloud_firestore: ^0.14.0 to up you can check if a particular field exist in a documentSnapshot by doing this:

CollectionReference users = FirebaseFirestore.instance.collection('users');
var doc = await users.doc(id).get();
 if(doc.exists){
     Map<String, dynamic> map = doc.data();
     if(map.containsKey('field')){// Replace field by the field you want to check.
       var valueOfField = map['field'];
     }
   }

or you can also do this:

await _users.doc(id).get().then((doc){
   if(doc.exists){
     doc.data().forEach((key, value) { 
       if(key == 'field'){
         var valueOfField = value;
       }
     });
   }
});
like image 93
shin Avatar answered Nov 08 '22 12:11

shin