Flutter/Dart JSON and serialization of an existing library class Flutter/Dart JSON and serialization of an existing library class dart dart

Flutter/Dart JSON and serialization of an existing library class


Put this code in your model

to get the information from JSON response simply do that after your request

  1. final place = placeFromJson(response.body);
  2. to get address => = place.address
  3. to get coordinates => place.coordinates.lng , place.coordinates.lat

=============================================

import 'dart:convert';Place placeFromJson(String str) => Place.fromJson(json.decode(str));String placeToJson(Place data) => json.encode(data.toJson());class Place {    String address;    Coordinates coordinates;    Place({        this.address,        this.coordinates,    });    factory Place.fromJson(Map<String, dynamic> json) => Place(        address: json["address"],        coordinates: Coordinates.fromJson(json["coordinates"]),    );    Map<String, dynamic> toJson() => {        "address": address,        "coordinates": coordinates.toJson(),    };}class Coordinates {    String lat;    String lng;    Coordinates({        this.lat,        this.lng,    });    factory Coordinates.fromJson(Map<String, dynamic> json) => Coordinates(        lat: json["lat"],        lng: json["lng"],    );    Map<String, dynamic> toJson() => {        "lat": lat,        "lng": lng,    };}


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;}


I didn't find a super easy solution for this. I ended up, writing a custom toJson/fromJson for the LatLng property. I've put it statically onto my model, but you could also create a global function, if you need to reuse it.

Take care, that in my example LatLng is nullable.

import 'dart:collection';import 'dart:convert';import 'package:latlong2/latlong.dart';part 'challenge_model.g.dart';@JsonSerializable()class ChallengeModel with _$ChallengeModel {  ChallengeModel({    required this.startPosition,  });  @override  @JsonKey(fromJson: latLngFromJson, toJson: latLngToJson)  final LatLng? startPosition;  /// Create [ChallengeModel] from a json representation  static fromJson(Map<String, dynamic> json) => _$ChallengeModelFromJson(json);  /// Json representation  @override  Map<String, dynamic> toJson() => _$ChallengeModelToJson(this);  static String latLngToJson(LatLng? latLng) =>      jsonEncode(latLng != null ? {'latitude': latLng.latitude, 'longitude': latLng.longitude} : null);  static LatLng? latLngFromJson(String jsonString) {    final LinkedHashMap<String, dynamic>? jsonMap = jsonDecode(jsonString);    return jsonMap != null ? LatLng(jsonMap['latitude'], jsonMap['longitude']) : null;  }}