Marcus.L
Marcus.L

Reputation: 103

How to check if firestore database document exist when given a document id?

I want to check if a document in firestore exist when given a document id. So far I have tried this:

String getUserType(String uid) {
final result = Firestore.instance.collection('patients').document(uid).get();
if (result == null) {
  return 'null';
} else {
  return 'exist';
}    

Upvotes: 0

Views: 439

Answers (1)

Zvi Karp
Zvi Karp

Reputation: 3904

you can use result.exists.

original post: https://stackoverflow.com/a/56465899/4465386

final result = await Firestore.instance
  .collection('posts')
  .document(docId)
  .get()

if (result == null || !result.exists) {
  // Document with id == docId doesn't exist.
}

Upvotes: 2

Related Questions