I need to add field to document when key is the Firebase/Firestore server timestamp. The only working solution i found till now is first create record with timestamp as value with FieldValue.serverTimestamp()
, than read it from Firestore and than use received value for a key. This solution looks ugly and I don’t think it is correct one.
Using FieldValue.serverTimestamp().toString()
as key, add “FieldValue(Instance of ‘MethodChannelFieldValue’)”string instead of actual timestamp value.
The code looks like:
saveUserAnswersToFirestore(List<RemoteAnswerModel> answers) async {
User? user = FirebaseAuth.instance.currentUser;
DocumentReference<Map<String, dynamic>> userAnswerDoc = FirebaseFirestore.instance
.collection('answers')
.doc('${user?.phoneNumber}');
final now = '${Timestamp.now().millisecondsSinceEpoch}'; //FieldValue.serverTimestamp().toString();
var answersMap = answers.map((e) => e.toJson()).toList();
try {
await userAnswerDoc.update({
now : answersMap
});
} catch (e) {
await userAnswerDoc.set({
now : answersMap
});
}
}
Any thoughts?
This is how the result should look like: