Flutter Firestore Server side Timestamp

I need to add a server side timestamp on new documents added to Firestore using a Flutter app. I see I am supposed to use FieldValue.serverTimestamp but I am not sure where to find this.


Solution 1:

As of September 5th, the updated cloud_firestore v0.8.0 library now has FieldValue.serverTimestamp(). All is now well in the universe

Solution 2:

'timestamp' : Timestamp.now()

Timestamp.now() is part of cloud_firestore;


Example enter image description here Screenshot showing that the library is imported from cloud_firestore, and creates a server-generated timestamp in the written data. Docs

Solution 3:

Expanding on @spongyboss' answer (which works as of April 2020) by adding sample usage:

_firestore.collection('messages').add({
                      'text': messageText,
                      'sender': loggedInUser.email,
                      'created': FieldValue.serverTimestamp()
                  });

'created' will be stored as a timestamp

Sample sorting:

_firestore.collection('messages')
          .orderBy('created', descending: false)
          .snapshots()