5

I know this is duplication but since I haven't been able to find correct way so please don't mark this as duplicate. I would like to set date type correctly to Firestore in flutter. I tried DateTime.now() but when I retrieve it error says

Unsupported value: FIRTimestamp: seconds=1533950401 nanoseconds=81000000> of type FIRTimestamp

and app will crash. Also I tried millisecondsSinceEpoch, microsecondsSinceEpoch, toUtc but Firestore recognize those as number not timestamp. I used intl package and formatted like yyyy-MM-dd hh:mm but it was recognized as String. Does anyone know how to set timestamp correctly?

Daibaku
  • 11,416
  • 22
  • 71
  • 108

2 Answers2

5

Use

FieldValue.serverTimestamp()
Farick Mena
  • 322
  • 3
  • 9
3

The Firestore database only has a Timestamp field record. The following worked and saved the timestamp properly.

Firestore.instance.collection('COLLECTION_NAME').add({
   'weight': someDoubleValue,
   'when': DateTime.now(),
});

This is with

firebase_core: ^0.2.5
cloud_firestore: ^0.8.2
Matt Glaman
  • 239
  • 3
  • 14