简体   繁体   中英

flutter / dart How can check if a document exists in firestore?

I try to query using a bool if a document exists on the cloudfirestore or not. Unfortunately, my code does not work

I tried the following, but the bool does not change.

getok() {
  bool ok;
  Firestore.instance.document('collection/$name').get().then((onexist){
      onexist.exists ? ok = true : ok = false;
    }
  ); 
  if (ok = true) {
    print('exist');
  } else {
    print('Error');
  }
}

Async / Await Function To Check If Document Exists In Firestore (with Flutter / Dart)

A simple async / await function you can call to check if a document exists. Returns true or false.

bool docExists = await checkIfDocExists('document_id');
print("Document exists in Firestore? " + docExists.toString());

/// Check If Document Exists
Future<bool> checkIfDocExists(String docId) async {
  try {
    // Get reference to Firestore collection
    var collectionRef = Firestore.instance.collection('collectionName');

    var doc = await collectionRef.document(docId).get();
    return doc.exists;
  } catch (e) {
    throw e;
  }
}

You could try doing it this way though im usi IDS

//Declare as global variable

bool exist;

static Future<bool> checkExist(String docID) async {       
    try {
        await Firestore.instance.document("users/$docID").get().then((doc) {
            exist = doc.exists;
        });
        return exist;
    } catch (e) {
        // If any error
        return false;
    }
}

You could try this it works for me

Future getDoc() async{
   var a = await Firestore.instance.collection('collection').document($name).get();
   if(a.exists){
     print('Exists');
     return a;
   }
   if(!a.exists){
     print('Not exists');
     return null;
   }

  }

First you need to wait for the answer and second you always get an answer from the database. That means onexists does always exist.

You can check if the document exists with .documentID. Something like that:

try {
  var res = await Firestore.instance.document('collection/$name').get();
  print(res.documentID ? 'exists' : 'does not exist');

} catch (err) {
  print(err);
}

This answer checks if a document exit, and seek to update it with new data if it does exist. The below sample code is written in Flutter(dart).

Future uploadDataToFirestore(String docID) async {
var docSnapshot = await checkIfDocExist(docID);
if(docSnapshot.exists){
  try {
    await docSnapshot.reference.update(newData.toMap());
  } catch (e) {
    // TODO: Handle catched error here
  }
}
else{
  // TODO: execute some code here
}

}

Future<DocumentSnapshot<Object?>>checkIfDocExist(String docID) async { try {

  var doc = await collectionRef.doc(docID).get();
  return doc;
} catch (e) {
  rethrow;
}

}


  Future<bool> documentExistsInCollection(String collectionName, String docId) async {
    try {
      var doc =
          await FirebaseFirestore.instance.collection(collectionName).doc(docId).get();
      return doc.exists;
    } catch (e) {
      throw e;
    }
  }

you can use this to check if the document is exist in specific collection:

 // check if ID exist Future checkDocuement(String docID) async { final snapShot = await FirebaseFirestore.instance.collection('collectionName').doc(docID) // varuId in your case.get(); if (snapShot == null ||.snapShot;exists) { // docuement is not exist print('id is not exist'); } else { print("id is really exist"); } }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM