Im writing an app in flutter, and im trying to make a messages document that stores a list of messages using the following:
Future<void> updateStingrayMessageForLike(
String chatId, String? stingrayid) async {
return FirebaseFirestore.instance
.collection('stingrays')
.doc(stingrayid)
.collection('messages')
.doc(chatId)
.set({'messages': []}, SetOptions(merge: true))
.then((value) => print("User Updated"))
.catchError((error) => print("Failed to update user: $error"));
}
The problem is, this method is called in a case where a user enters a message screen, but a message document has not been created yet. So if the user adds messages, leaves the screen, then comes back, the document will reset its messages to an empty array object. Is there a way to only create/update a document if does not exist?