I\'m fetching data cloud firestore & trying to show in my app by using the following piece of code.
new Text(timeago.format(document.data[\'tripDoc\'][\'
Try document.data["data"].microsecondsSinceEpoch
This is working for me:-
User.fromDocument(DocumentSnapshot document){
dataNasc = DateTime.fromMicrosecondsSinceEpoch(document.data["data"].microsecondsSinceEpoch);
}
DateTime.fromMillisecondsSinceEpoch(timeStamp);
DateTime.fromMicrosecondsSinceEpoch(timeStamp);
If you use JsonSerializable, Use JsonConverter
class TimestampConverter implements JsonConverter<DateTime, Timestamp> {
const TimestampConverter();
@override
DateTime fromJson(Timestamp timestamp) {
return timestamp.toDate();
}
@override
Timestamp toJson(DateTime date) => Timestamp.fromDate(date);
}
@JsonSerializable()
class User{
final String id;
@TimestampConverter()
final DateTime timeCreated;
User([this.id, this.timeCreated]);
factory User.fromSnapshot(DocumentSnapshot documentSnapshot) =>
_$UserFromJson(
documentSnapshot.data..["_id"] = documentSnapshot.documentID);
Map<String, dynamic> toJson() => _$UserToJson(this)..remove("_id");
}
The Cloud Firebase timestamp field type has the structure:
Timestamp (Timestamp(seconds=1561186800, nanoseconds=0))
hashCode:423768083
microsecondsSinceEpoch:1561186800000000
millisecondsSinceEpoch:1561186800000
nanoseconds:0
runtimeType:Type (Timestamp)
seconds:1561186800
_seconds:1561186800
_nanoseconds:0
So you can use either micro- or milliseconds:
DateTime.fromMillisecondsSinceEpoch(data['tripDoc']['docCreatedOn'].millisecondsSinceEpoch)
or
DateTime.fromMicrosecondsSinceEpoch(data['tripDoc']['docCreatedOn'].microsecondsSinceEpoch)
You can try this..
timeago.format(DateTime.tryParse(timestamp))
like in yours it will be
timeago.format(DateTime.tryParse(document.data['tripDoc']['docCreatedOn']))
For some funny reason, I can't use toDate() on Android. Have to use it for iOS. So I'm forced to use a platform check like this:
Theme.of(context).platform == TargetPlatform.iOS
? DateFormat('dd MMM kk:mm').format(document['timestamp'].toDate())
: DateFormat('dd MMM kk:mm').format(document['timestamp'])