listCollections method

Fetches the subcollections that are direct children of this document.

final documentRef = firestore.doc('col/doc');

documentRef.listCollections().then((collections) {
  for (final collection in collections) {
    print('Found subcollection with id: ${collection.id}');
  }
});

Implementation

Future<List<CollectionReference<DocumentData>>> listCollections() {
  return this.firestore._client.v1((a) async {
    final request = firestore1.ListCollectionIdsRequest(
      // Setting `pageSize` to an arbitrarily large value lets the backend cap
      // the page size (currently to 300). Note that the backend rejects
      // MAX_INT32 (b/146883794).
      pageSize: (math.pow(2, 16) - 1).toInt(),
    );

    final result = await a.projects.databases.documents.listCollectionIds(
      request,
      this._formattedName,
    );

    final ids = result.collectionIds ?? [];
    ids.sort((a, b) => a.compareTo(b));

    return [
      for (final id in ids) collection(id),
    ];
  });
}