How can I save two records with the id of the other in flutter?
I’m trying to save two records with id of the other, I can do that, but when I try to save more than 2 at same time some id come with blank. This is my code:
collectionReferenceRel.add({
'idRoom': id,
'room': rel,
'rel': room,
'id': '',
}).then((idRel) {
idRel1 = idRel.id;
},
);
collectionReferenceRel.add({
'idRoom': id,
'room': room,
'rel': rel,
'id': '',
}).then((value2) {
idNode2 = value2.id;
}).whenComplete(() async {
await collectionReferenceRel.doc(idRel1).update({
'id': idRel2,
});
await collectionReferenceRel.doc(idRel2).update({
'id': idRel1,
});
}).catchError((error) {
CustomFullScreenDialog.cancelDialog();
CustomSnackBar.showSnackBar(
context: Get.context,
title: 'Error',
message: 'Something went wrong',
backgroundColor: Colors.green);
[![enter image description here][1]][1] },
);
2
Answers
https://api.flutter-io.cn/flutter/dart-async/Future/wait.html
Consider using the
set()
method provided the cloud firestore api.Usage Example from the reference.
For saving more than one document consider coupling it with the
Future
wait
for a clean code.