sungkd123
sungkd123

Reputation: 403

Upload Date and time from firestore in Flutter

I have created a collection named Data where the user data is stored. While displaying the data i want to display the date and time it was uploaded on the server. Is it possible to get the date and time it was uploaded on Firestore? If yes how to do it?

P.S. I don't want to have two fields for date and time in my collection that's why looking for an alternative.

Upvotes: 1

Views: 1097

Answers (1)

Frank van Puffelen
Frank van Puffelen

Reputation: 598887

Firestore doesn't expose the date/time a document was created, so you will at the very least have to add one field for that.

But the entire value can be stored in a single field of type Timestamp, which identifies a point in time. If you want to set a field to the current timestamp on the server, you can do:

collection
      .add({
        'full_name': fullName, // John Doe
        'company': company, // Stokes and Sons
        'age': age // 42
        'createdAt': FieldValue.serverTimestamp()
      })

Upvotes: 1

Related Questions