-
-
Notifications
You must be signed in to change notification settings - Fork 286
Description
Hi, I have a large app using built_value and I'm migrating it to using freezed (and with that json_serializable). This would allow me to cut out a huge amount of boilerplate and give me all the lovely freezed features like deep copy etc.
The app has data models that are shared between the client and server. When they are serialized to/from Firestore the DateTime objects are converted to Timestamps. With built_value I have two sets of serializers, one results in pure Dart DateTime objects and the other (firestoreSerializers) handles the pesky Timestamps, as these are passed to and from the json methods I have the flexibility to use the necessary serializers based on the context:
// Current solution of passing serializers (with built_value)
FooBar.fromJsonMap(
Serializers serializers, Map<String, dynamic> data) =>
serializers.deserializeWith(ChatMessage.serializer, data)!;
Map<String, dynamic> toJsonMap(Serializers serializers) =>
Map.of(serializers.serialize(this, //....
Is there a way to accomplish something similar to this with freezed? I basically need handle the serialization & deserialization of DateTime's differently according to context requirements? There are situations where both the client and server need to serialize & deserialize objects to/from firestore as well as none-Firestore contexts (sending/receiving the objects with cloud-endpoint, caching etc).
I've looked at using package:flutterfire_json_converters which seems to address my issue but then there isn't any way to handle dart only environments.
Thanks