allOfClass<T> method

Future<List<T>> allOfClass<T>(
  1. Type type, {
  2. String? subcollection,
})

Lists all items of a specific type.

Implementation

Future<List<T>> allOfClass<T>(Type type, { String? subcollection }) async {
  if (T.toString() != type.toString()) {
    throw ArgumentError("Type mismatch. Attempting to list items of type '${T.toString()}', but parameter type was ${type.toString()}");
  }

  final deserializer = FS.deserializers[type];
  if (deserializer == null) {
    throw UnsupportedError('No deserializer found for type: $type. Consider re-generating Firestorm data classes.');
  }

  var collectionReference = FS.instance.collection(type.toString());
  if (subcollection != null) {
    collectionReference = collectionReference.doc(subcollection).collection(subcollection);
  }
  final Query<Map<String, dynamic>> query = collectionReference;
  var querySnapshot = await query.get();
  List<T> objects = [];

  //TODO - Consider the use of Multithreading
  for (var doc in querySnapshot.docs) {
    if (doc.exists) {
      objects.add(deserializer(doc.data()));
    }
  }
  return objects;
}