Advertisement
Guest User

Untitled

a guest
Feb 24th, 2022
253
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.29 KB | None | 0 0
  1. import 'package:json_annotation/json_annotation.dart';
  2.  
  3. part 'location.g.dart';
  4.  
  5. enum LocationType {
  6. @JsonValue('City')
  7. city,
  8. @JsonValue('Region')
  9. region,
  10. @JsonValue('State')
  11. state,
  12. @JsonValue('Province')
  13. province,
  14. @JsonValue('Country')
  15. country,
  16. @JsonValue('Continent')
  17. continent
  18. }
  19.  
  20. @JsonSerializable()
  21. class Location {
  22. const Location({
  23. required this.title,
  24. required this.locationType,
  25. required this.latLng,
  26. required this.woeid,
  27. });
  28.  
  29. // factory Location.fromJson(Map<String, dynamic> json) =>
  30. // _$LocationFromJson(json);
  31.  
  32. final String title;
  33. final LocationType locationType;
  34. @JsonKey(name: 'latt_long')
  35. @LatLngConverter()
  36. final LatLng latLng;
  37. final int woeid;
  38. }
  39.  
  40. class LatLng {
  41. const LatLng({required this.latitude, required this.longitude});
  42.  
  43. final double latitude;
  44. final double longitude;
  45. }
  46.  
  47. class LatLngConverter implements JsonConverter<LatLng, String> {
  48. const LatLngConverter();
  49.  
  50. @override
  51. String toJson(LatLng latLng) {
  52. return '${latLng.latitude},${latLng.longitude}';
  53. }
  54.  
  55. @override
  56. LatLng fromJson(String jsonString) {
  57. final parts = jsonString.split(',');
  58. return LatLng(
  59. latitude: double.tryParse(parts[0]) ?? 0,
  60. longitude: double.tryParse(parts[1]) ?? 0,
  61. );
  62. }
  63. }
  64.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement