6

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!

3 Answers 3

2

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;
}
Sign up to request clarification or add additional context in comments.

1 Comment

This is the answer ^
0

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

1 Comment

Thank you! The problem is that I need to use LatLng class. Can I use it and have my Place class serializable?
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.