many<T> method

Future<List<T>> many<T>(
  1. List<String> documentIDs, {
  2. String? subcollection,
})

Reads multiple documents from Firestore and converts them to a list of the specified type. //TODO - Consider the use of Multithreading.

Implementation

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

  for (String documentID in documentIDs) {
    String path = RDB.constructPathForClassAndID(T, documentID, subcollection: subcollection);
    paths.add(path);
  }

  //Convert paths to a list of futures, process simultaneously, and wait for all to complete:
  Iterable<Future<dynamic>> futures = documentIDs.map((documentID) => one<T>(documentID, subcollection: subcollection));

  List<dynamic> list = await Future.wait(futures);
  for (var object in list) {
    if (object != null) {
      if (object is T) {
        objects.add(object);
      } else {
        throw UnsupportedError(
            'Expected type $T but got ${object.runtimeType}');
      }
    }
  }
  return objects;
}