I have a freezed class that looks like this:
@freezed
abstract class GiftGiver with _$GiftGiver {
const factory GiftGiver({
String? id,
String? uid,
String? imageUrl,
String? giftDetails,
String? listingDate,
@Default(5) int listingFor,
Timestamp? pickUpTime,
@Default(false) canLeaveOutside,
}) = _GiftGiver;
factory GiftGiver.fromJson(Map<String, dynamic> json) =>
_$GiftGiverFromJson(json);
}
The freezed class is generated fine but .g.dart class is not generated as I have Timestamp type. I saw some solution at https://github.com/rrousselGit/freezed#fromjson---classes-with-multiple-constructors but I am not understanding how to apply it to solve my problem.
After some research I found out the solution. For types that are not supported by JsonSerializable I needed to use JsonKey to create my own toJson and fromJson method. I am attaching one such class that has Timestamp and Also another nested class (MyPosition) inside it.
GiftGiver class uses MyPosition (another Freezed class) which looks like this =>
To use MyPosition in GiftGiver properly I needed to create _toJson and _fromJson and tell GIftGiver how to decode and encode MyPosition field.