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 = RDB.deserializers[type];
  if (deserializer == null) {
    throw UnsupportedError('No deserializer found for type: $type. Consider re-generating Firestorm data classes.');
  }

  List<T> objects = [];
  final String path = RDB.constructPathForClass(type, subcollection: subcollection);
  final Query query = RDB.instance.ref(path);
  final DataSnapshot snapshot = await query.get();
  if (snapshot.exists) {
    for (final childSnapshot in snapshot.children) {
      if (childSnapshot.exists) {
        final Map<String, dynamic> map = RDBDeserializationHelper.snapshotToMap(childSnapshot);
        objects.add(deserializer(map));
      }
    }
  }
  return objects;
}