I am trying to update my firestore doc from different screens. But when i use .set with merge true or even use update the previous values gets assigned to null. Please help me to sort out this problem
addDataToDBSubCollection(String collectionName, String docID,
String subCollection, String subDocID, Map<String, dynamic> data) async {
bool lCheck = false;
try {
lCheck = await _db
.collection(collectionName)
.doc(docID)
.collection(subCollection)
.doc(subDocID)
.set(data, SetOptions(merge: true))
.then((value) {
print("true");
return true;
});
} on FirebaseException catch (e) {
// Caught an exception from Firebase.
print("Failed with error '${e.code}': ${e.message}");
lCheck = false;
}
return lCheck;
}
updateDataToDBSubCollection(String collectionName, String docID,
String subCollection, String subDocID, Map<String, dynamic> data) async {
bool lCheck = false;
try {
lCheck = await _db
.collection(collectionName)
.doc(docID)
.collection(subCollection)
.doc(subDocID)
.update(data)
.then((value) {
print("true");
return true;
});
} on FirebaseException catch (e) {
// Caught an exception from Firebase.
print("Failed with error '${e.code}': ${e.message}");
lCheck = false;
}
return lCheck;
}
This is the way i am assigning the values from different screens
Screen1
_checkTimingsModel = CheckTimingsModel(
id: id,
userName: "Pulkit",
checkedInAt: _homeController.cInDateTime,
checkedOutAt: null);
Screen 2
_checkTimingsModel = CheckTimingsModel(
oilImagePath: oilURL,
coolantImagePath: coolantURL,
weeklyTaskDate: DateTime.now(),
);
And then i called the methods
_dbServices.addDataToDBSubCollection(
"users",
phn!,
"checkTimeDetails",
id,
_checkTimingsModel!.toMap());
This funtion when called on Screen 2 is replacing the values of screen 1 with null and screen 2 values get inserted. Pleas help me to resolve this problem.
Recognized by Google Cloud Collective
3