I am trying to convert a firebase timestamp that has been returned from a could function to a date object in flutter.
I already tried those solutions but they didn't work: Converting timestamp
Here is the code I am using:
await firebaseFunctions
.httpsCallable('functionName')
.call({"data": data}).then((value) {
Map<String, dynamic> map = value.data;
DateTime dt = (map['dateOfPurchase'] as Timestamp).toDate();
print(dt);
});
The data
that the map['dateOfPurchase']
attribut containes: {_nanoseconds: 665000000, _seconds: 1659266068}
The exception
i get :
[ERROR:flutter/lib/ui/ui_dart_state.cc(198)] Unhandled Exception: type '_InternalLinkedHashMap<Object?, Object?>' is not a subtype of type 'Timestamp' in type cast
CodePudding user response:
I found a solution to my own problem with this code
await firebaseFunctions
.httpsCallable('functionName')
.call({"data": data}).then((value) {
final map = Map<String, dynamic>.from(value.data['dateOfPurchase']);
final seconds = map["_seconds"];
final nanoseconds = map["_nanoseconds"];
final Timestamp postConverted = Timestamp(seconds, nanoseconds);
print(postConverted.toDate());
});
Result of the print
: 2022-07-31 13:14:28.665
I hope that could help some people who face the same problem then me.
If someone has a better solution don't hesitate to share it.