I want to save data on Firebase Firestore where the key will be timestamp:
await FirebaseFirestore.instance
.collection("users")
.doc(uid)
.collection('data')
.doc(device.id)
.update({
DateTime.now().toString(): "${result['humidity']}_${result['tempInC']}"
});
}
What can be the possible reasons for this nested hierarchy as shown in the image: Screenshot, any solution?
2
Answers
Using milli_seconds_since_epoch is better alternative in such scenarios.
According to my first question in the comments section:
And your answer:
Please note that there is no way you can save a Date/Timestamp object as a key in a Firestore document. The keys are always strings. You can indeed convert the Date object into a String, but I don’t think this is what you’re looking for.
The best option I can think of would be to save the timestamp as a value and not as a key. For example, you can add a field called
addedAt
that should hold a Firestore timestamp. In this way, you’ll be able to query the collection using Timestamp objects.