duplicate_document function
dynamic
duplicate_document({})
Implementation
duplicate_document({
required String document_id,
required String collection_id,
required int times,
String? base_id,
required bool apply_random_number,
}) async {
CollectionReference<Map<String, dynamic>> collection_reference =
FirebaseFirestore.instance.collection(collection_id);
await FirebaseFirestore.instance
.collection(collection_id)
.doc(document_id)
.get()
.then((document_snapshot) {
if (document_snapshot.data() != null) {
for (var i = 0; i < times; i++) {
if (base_id != null) {
String counter = times == 1 ? "" : "_" + ((i + 1).toString());
if (apply_random_number) {
int random_numer_1 = random_number_with_range(0, 9);
int random_numer_2 = random_number_with_range(0, 9);
int random_numer_3 = random_number_with_range(0, 9);
int random_numer_4 = random_number_with_range(0, 9);
String random_numer =
"$random_numer_1$random_numer_2$random_numer_3$random_numer_4";
counter = "_$random_numer$counter";
}
String doc_name = "$base_id$counter";
print(doc_name);
collection_reference.doc(doc_name).set(document_snapshot.data()!);
} else {
collection_reference.add(document_snapshot.data()!);
}
}
}
});
}