sajanyamaha
sajanyamaha

Reputation: 3198

check if firebase record exist and return bool

Trying to check firebase record and perform subsequent logics,

Future<bool> isAlreadyThere(selectedPropertyId) async {
 final FirebaseUser user = await FirebaseAuth.instance.currentUser();

 var myMapQuery = Firestore.instance
    .collection("props")
    .where('xxx', isEqualTo: xxxid)
    .where('yyy', isEqualTo: user.uid);
 var querySnapshot=  await myMapQuery.getDocuments();
 var totalEquals= querySnapshot.documents.length;
 return totalEquals > 0;
}

and in the onTap() of widget ,

bool isThere=isAlreadyThere(suggestion.documentID) as bool;
 if (isThere) {//do stuff here}

errors, type 'Future' is not a subtype of type 'bool' in type cast

I know its the casting , but tried removing and some other ways as well , wont work.

Upvotes: 0

Views: 164

Answers (1)

Jitesh Mohite
Jitesh Mohite

Reputation: 34250

await is missing in where the query

Future<bool> isAlreadyThere(selectedPropertyId) async {
 final FirebaseUser user = await FirebaseAuth.instance.currentUser();

 var myMapQuery = (await Firestore.instance
    .collection("props")
    .where('xxx', isEqualTo: xxxid)
    .where('yyy', isEqualTo: user.uid));
   var querySnapshot=  await myMapQuery.getDocuments();
   var totalEquals= querySnapshot.documents.length;
   return totalEquals > 0;
}

Use method like

isAlreadyThere(suggestion.documentID).then((value) {
     if (value) {//do stuff here}
    });

Upvotes: 1

Related Questions