I am writing several documents in my application following the acceptance of a “reservation”.
So I’m executing the following function, with a batch.
Sometimes it executes correctly, sometimes it takes more than 10 minutes to execute, on the same device, same network.
Has anyone had a similar problem? How can I solve it? Or where can I find a way to diagnose it?
Thanks in advance !
Future<void> accepterReservation() async {
print('accepter resa');
setState(() {
accepterResaIsLoading = true;
});
ChatsRecord docChatMAJ =
await ChatsRecord.getDocumentOnce(widget.chatDoc!.reference);
int? prixLoc = docChatMAJ.prix;
DateTime? dateDebut = docChatMAJ.dateDebut;
DateTime? dateFin = docChatMAJ.dateFin;
int daysDifference = dateFin!.difference(dateDebut!).inDays;
if (docChatMAJ?.offrePrix != null && docChatMAJ?.offrePrix != 0) {
setState(() {
_model.prixCommunique = docChatMAJ?.offrePrix;
});
print(_model.prixCommunique);
} else {
print('ici');
_model.prixCommunique = docChatMAJ?.prix;
print(_model.prixCommunique);
}
String baseTextP =
'La réservation sera validée une fois que le locataire aura réglé le montant de la location.';
String baseTextL =
'Effectue le paiement de ta location pour valider la réservation.';
if (daysDifference > 6) {
baseTextP +=
'nLoomi ne prend pas en charge la caution pour les locations de plus de 6 jours, nous t'invitons à te rapprocher du locataire pour sa mise en place.';
baseTextL +=
'nLoomi ne prend pas en charge la caution pour les locations de plus de 6 jours, nous t'invitons à te rapprocher du propriétaire pour sa mise en place.';
}
WriteBatch batch = FirebaseFirestore.instance.batch();
// On crée le message pour le locataire
DocumentReference newChatMsgLoc = ChatMessagesRecord.collection.doc();
batch.set(
newChatMsgLoc,
createChatMessagesRecordData(
type: 'notification_reservation',
destinataire: docChatMAJ.locataire,
titre:
'Le propriétaire a accepté ton offre de location du ${dateTimeFormat('dd/MM/y', dateDebut)} au ${dateTimeFormat('dd/MM/y', dateFin)} pour un montant de ${(fromCentimesToEuros(_model.prixCommunique)! / 1.18).toStringAsFixed(2)}€.',
timestamp: getCurrentTimestamp,
chat: docChatMAJ.reference,
text: baseTextL,
),
);
// On crée le message pour le propriétaire
DocumentReference newChatMsgProp = ChatMessagesRecord.collection.doc();
batch.set(
newChatMsgProp,
createChatMessagesRecordData(
destinataire: docChatMAJ.proprietaire,
titre:
'Tu as accepté la demande de location du ${dateTimeFormat('dd/MM/y', dateDebut)} au ${dateTimeFormat('dd/MM/y', dateFin)} pour un montant de ${(fromCentimesToEuros(_model.prixCommunique)! / 1.18).toStringAsFixed(2)}€.',
text: baseTextP,
type: 'notification_info',
timestamp: getCurrentTimestamp,
chat: docChatMAJ.reference,
),
);
// On mets à jour le chat
batch.update(
widget.chatDoc!.reference,
{
'lastMessage': 'Offre de location acceptée',
'lastMessageTime': getCurrentTimestamp,
'prix': _model.prixCommunique,
'date_debut': dateDebut,
'date_fin': dateFin,
'statutLocation': 'Demande acceptée',
'last_message_seen_by': FieldValue.arrayUnion([currentUserReference]),
},
);
// Supprimer le message de demande de réservation
batch.delete(widget.chatMessagesDoc!.reference);
// Suppression du message permettant au locataire d'annuler sa demande
if (widget.chatMessagesDoc!.msgAssocie != null) {
batch.delete(widget.chatMessagesDoc!.msgAssocie!);
}
try {
batch.commit();
print('fin du batch');
} catch (e) {
print('Erreur lors de l'exécution du batch : $e');
}
print('fin du batch2');
setState(() {
accepterResaIsLoading = false;
_model.lastSeenBy = [];
});
}
Initially, I had put an await in my code, but then all my firebase requests stopped working in my application, and it became usable.
So I removed the await, but that doesn’t solve the problem of my request taking 10 minutes for no reason. I have the same problems in several part of my application, but it’s happening randomly, I can’t find the root cause of the problem.
Marie Dufau is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.