Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import 'package:json_annotation/json_annotation.dart';
- part 'location.g.dart';
- enum LocationType {
- @JsonValue('City')
- city,
- @JsonValue('Region')
- region,
- @JsonValue('State')
- state,
- @JsonValue('Province')
- province,
- @JsonValue('Country')
- country,
- @JsonValue('Continent')
- continent
- }
- @JsonSerializable()
- class Location {
- const Location({
- required this.title,
- required this.locationType,
- required this.latLng,
- required this.woeid,
- });
- // factory Location.fromJson(Map<String, dynamic> json) =>
- // _$LocationFromJson(json);
- final String title;
- final LocationType locationType;
- @JsonKey(name: 'latt_long')
- @LatLngConverter()
- final LatLng latLng;
- final int woeid;
- }
- class LatLng {
- const LatLng({required this.latitude, required this.longitude});
- final double latitude;
- final double longitude;
- }
- class LatLngConverter implements JsonConverter<LatLng, String> {
- const LatLngConverter();
- @override
- String toJson(LatLng latLng) {
- return '${latLng.latitude},${latLng.longitude}';
- }
- @override
- LatLng fromJson(String jsonString) {
- final parts = jsonString.split(',');
- return LatLng(
- latitude: double.tryParse(parts[0]) ?? 0,
- longitude: double.tryParse(parts[1]) ?? 0,
- );
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement