Flutter/Dart JSON and serialization of an existing library class

560

You can explicitly specify which methods of LatLng should be used for serialization using JsonKey annotation:

@JsonSerializable()
class Place {
  Place({
    this.address,
    this.coordinates,
  });

  final String address;
  @JsonKey(fromJson: LatLng.fromJson, toJson: jsonEncode)
  final LatLng coordinates;
}
Share:
560
Marco
Author by

Marco

Updated on December 17, 2022

Comments

  • Marco
    Marco over 1 year

    I have a class:

    import 'package:google_maps_flutter/google_maps_flutter.dart';
    
    class Place {
      Place({
        this.address,
        this.coordinates,
      });
    
      final String address;
      final LatLng coordinates;
    }
    

    LatLng is a class of google_maps_flutter. How can I make my Place class serializable using json_annotation and json_serializable?

    Thank you very much!