I have a freezed class with the following fields:
import 'package:freezed_annotation/freezed_annotation.dart';
import 'package:latlong2/latlong.dart';
part 'event.freezed.dart';
part 'event.g.dart';
@freezed
class Event with _$Event {
const factory Event({
required LatLng location,
required DateTime date,
required String name,
}) = _Event;
factory Event.fromJson(Map<String, Object?> json) => _$EventFromJson(json);
}
Notice that the LatLng object comes from a package called latlong2. When I generate the code with build runner I get the following output.
// GENERATED CODE - DO NOT MODIFY BY HAND
part of 'event.dart';
// **************************************************************************
// JsonSerializableGenerator
// **************************************************************************
_$_Event _$$_EventFromJson(Map<String, dynamic> json) => _$_Event(
location: LatLng.fromJson(json['location'] as Map<String, dynamic>),
date: DateTime.parse(json['date'] as String),
name: json['name'] as String,
);
Map<String, dynamic> _$$_EventToJson(_$_Event instance) => <String, dynamic>{
'location': instance.location,
'date': instance.date.toIso8601String(),
'name': instance.name,
};
For some reason json_serializable properly decodes the object but is unable to encode it because it doesn't call toJson() on instance.location. How did I fix this?