I’m facing an issue with Firestore in my Flutter application. I’m trying to access a document in my “users” collection using the recipient’s userID, but I keep getting an error [cloud_firestore/not-found] Some requested document was not found. even though I’ve allowed read and write access in my Firestore rules.
Future<void> performTransaction(
BuildContext context, double montant, String recipientUserId) async {
String? senderUserId =
FirebaseAuth.instance.currentUser!.uid; // Utiliser le userID Firebase
// Exemple de code Dart pour obtenir un token d'accès Firebase
String? idToken = await FirebaseAuth.instance.currentUser?.getIdToken();
print(idToken);
// Vérifiez que le senderUserId n'est pas null
if (senderUserId == null) {
print('Sender UserID not found.');
return;
}
print('Sender UserID: $senderUserId');
print('Recipient UserID: $recipientUserId');
try {
// Référence au document de l'expéditeur (sender)
DocumentReference senderRef =
FirebaseFirestore.instance.collection('users').doc(senderUserId);
// Référence au document du destinataire (recipient) en utilisant recipientUserId
DocumentReference recipientRef =
FirebaseFirestore.instance.collection('users').doc(recipientUserId);
// Fetch sender document
DocumentSnapshot senderSnapshot = await senderRef.get();
print('Sender document: ${senderSnapshot.data()}');
// Fetch recipient document
DocumentSnapshot recipientSnapshot = await recipientRef.get();
print('Recipient document: ${recipientSnapshot.data()}');
// Vérifiez que les documents existent
if (!senderSnapshot.exists) {
print('Sender document not found: $senderUserId');
throw Exception('Sender not found');
}
if (!recipientSnapshot.exists) {
print('Recipient document not found: $recipientUserId');
throw Exception('Recipient not found');
}
await FirebaseFirestore.instance.runTransaction((transaction) async {
// Réobtenir les snapshots dans la transaction
DocumentSnapshot senderSnapshot = await transaction.get(senderRef);
DocumentSnapshot recipientSnapshot =
await transaction.get(recipientRef);
print('Current User ID: ${FirebaseAuth.instance.currentUser!.uid}');
print('Recipient User ID: $recipientUserId');
double senderBalance = senderSnapshot['balance'];
double recipientBalance = recipientSnapshot['balance'];
// Vérifiez que le solde de l'expéditeur est suffisant
if (senderBalance < montant) {
throw Exception('Insufficient funds');
}
// Mettre à jour les balances
transaction.update(senderRef, {'balance': senderBalance - montant});
transaction
.update(recipientRef, {'balance': recipientBalance + montant});
});
print('Transaction successful');
} catch (e) {
print('Transaction failed: $e');
}
}
Despite setting Firestore rules to allow read and write access, I continue to receive a document not found error when trying to access the recipient’s document using their userID (recipientUserId). I’ve verified the existence of the document in the Firestore collection.
I’ve double-checked the Firestore rules, ensured the correctness of the userID, and verified the document’s existence through Firestore console. Is there anything I might be missing or any alternative approaches to troubleshoot this issue effectively?