jsonToCollection method

Future<void> jsonToCollection({
  1. required String path,
  2. required JsonObject json,
  3. required OnParsedCallback onParsed,
  4. String? changeRootName,
})

Converts a JSON object to a Firestore collection and its documents.

This function recursively processes a JSON object representing a Firestore collection and its inner documents. It processes each document in the collection, converting it to a Firestore Document and calling the onParsed callback.

  • path: The path to the collection.
  • json: The JSON object to convert.
  • onParsed: The callback to call for each parsed document.

Implementation

Future<void> jsonToCollection({
  required String path,
  required JsonObject json,
  required OnParsedCallback onParsed,
  String? changeRootName,
}) async {
  final collectionName = changeRootName ?? (json[metaName] as String?) ?? '';
  if (collectionName.isEmpty) {
    throw FormatException(
      'Wrong `$metaName` value=`$collectionName` '
      'in json=`$json`',
    );
  }

  final collectionPath = pathUtils.join(path, collectionName);

  final innerDocuments = _jsonCollectionToDocuments(json);

  for (final innerDocument in innerDocuments) {
    await jsonToDocument(
      path: collectionPath,
      json: innerDocument,
      onParsed: onParsed,
    );
  }
}