one<T> method

Future<T?> one<T>(
  1. String documentID, {
  2. String? subcollection,
})

Reads a document from Firestore and converts it to the specified type.

Implementation

Future<T?> one<T>(String documentID, { String? subcollection }) async {
  final deserializer = FS.deserializers[T];
  if (deserializer == null) {
    throw UnsupportedError('No deserializer found for type: $T. Consider re-generating Firestorm data classes.');
  }

  DocumentReference ref = FS.instance.collection(T.toString()).doc(documentID);
  if (subcollection != null) {
    ref = FS.instance.collection(T.toString()).doc(subcollection).collection(subcollection).doc(documentID);
  }

  DocumentSnapshot snapshot = await ref.get();
  if (!snapshot.exists) {
    return null;
  }
  T object = deserializer(snapshot.data() as Map<String, dynamic>) as T;
  return object;
}