Timestamp from firestore gets converted to a Map when using cloud function

Indeed, timestamps gets returned as plain Map when using Cloud functions. But if you use Firebase SDK it returns Timestamp object. I use the following function to handle both cases:

/// https://stackoverflow.com/a/57865272/1321917
DateTime dateTimeFromTimestamp(dynamic val) {
  Timestamp timestamp;
  if (val is Timestamp) {
    timestamp = val;
  } else if (val is Map) {
    timestamp = Timestamp(val['_seconds'], val['_nanoseconds']);
  }
  if (timestamp != null) {
    return timestamp.toDate();
  } else {
    print('Unable to parse Timestamp from $val');
    return null;
  }
}

Works perfectly with json_annotation lib:

  @JsonKey(
      fromJson: dateTimeFromTimestamp,
      toJson: dateTimeToTimestamp,
      nullable: true)
  final DateTime subscriptionExpiryDate;

If you are dealing with a Timestamp that's been serialized as an object with seconds and nanoseconds components, you can use those components to create a new Timestamp object with new Timestamp(seconds, nanoseconds).