writeData method

Future<bool> writeData(
  1. String documentPath,
  2. Map<String, dynamic> data, {
  3. bool merge = true,
})

Writes data to a Firestore document.

  • documentPath: Path to the Firestore document.
  • data: The data to write.
  • merge: Whether to merge data with existing content (default: true).

Returns true if the operation is successful, false otherwise.

Implementation

Future<bool> writeData(String documentPath, Map<String, dynamic> data, {bool merge = true}) async {
  try {
    final documentSnapshot = await _firestore.doc(documentPath).get();

    if (documentSnapshot.exists) {
      await _firestore.doc(documentPath).update(data);
      debugPrint('Document updated successfully.');
    } else {
      await _firestore.doc(documentPath).set(data, SetOptions(merge: merge));
      debugPrint('Document created and data set successfully.');
    }
    return true;
  } catch (e) {
    debugPrint('Error writing data to Firestore: $e');
    return false;
  }
}