get<T extends FirestoreEntity<T>> static method

Future<T?> get<T extends FirestoreEntity<T>>(
  1. String id, {
  2. T orCreate()?,
})

Gets the document from Firestore. If the document does not exist, this method returns null.

Implementation

static Future<T?> get<T extends FirestoreEntity<T>>(String id, {T Function()? orCreate}) async {
  assert(DogFirestoreEngine.instance.checkRootCollection<T>());
  var documentReference = DogFirestoreEngine.instance.collection<T>().doc(id);
  var snapshot = await documentReference.get();
  if (snapshot.exists) {
    return snapshot.data()!;
  } else {
    if (orCreate != null) {
      var entity = orCreate();
      entity.id = id;
      return await entity.save();
    }
    return null;
  }
}