jsonToDocument method
Future<void>
jsonToDocument({
- required String path,
- required JsonObject json,
- required OnParsedCallback onParsed,
- String? changeRootName,
Converts a JSON object to a Firestore document and its inner collections.
This function recursively processes a JSON object representing a Firestore
document and its inner collections. It converts the JSON object to a
Firestore Document and then calls the onParsed callback with the
document's path, ID, and the document itself. It then processes any inner
collections recursively.
path: The path to the document or collection.json: The JSON object to convert.onParsed: The callback to call for each parsed document.
Implementation
Future<void> jsonToDocument({
required String path,
required JsonObject json,
required OnParsedCallback onParsed,
String? changeRootName,
}) async {
final (id, document) = _jsonToDocument(json);
final name = changeRootName ?? id;
await onParsed(path, name, document);
final rawDocumentCollections = json[metaCollections] ?? [];
if (rawDocumentCollections is! List<dynamic>) {
throw FormatException(
'Wrong `$metaCollections` value=`$rawDocumentCollections` '
'in json=`$json`',
);
}
final documentCollections = rawDocumentCollections.cast<JsonObject>();
final documentPath = pathUtils.join(path, name);
for (final documentCollection in documentCollections) {
final documentCollectionName =
documentCollection[metaName] as String? ?? '';
if (documentCollectionName.isEmpty) {
continue;
}
await jsonToCollection(
path: documentPath,
json: documentCollection,
onParsed: onParsed,
);
}
}