以下 json 是 Google Place Details Api 請求的回應正文。
{
"html_attributions": [],
"result": {
"address_components": [
{
"long_name": "18",
"short_name": "18",
"types": [
"street_number"
]
},
{
"long_name": "Doiranis",
"short_name": "Doiranis",
"types": [
"route"
]
},
{
"long_name": "Thessaloniki",
"short_name": "Thessaloniki",
"types": [
"locality",
"political"
]
},
{
"long_name": "Thessaloniki",
"short_name": "Thessaloniki",
"types": [
"administrative_area_level_3",
"political"
]
},
{
"long_name": "Greece",
"short_name": "GR",
"types": [
"country",
"political"
]
},
{
"long_name": "546 39",
"short_name": "546 39",
"types": [
"postal_code"
]
}
]
},
"status": "OK"
}
以下是我的Place模型類
import 'package:json_annotation/json_annotation.dart';
part 'place_model.g.dart';
@JsonSerializable(fieldRename: FieldRename.snake)
class Place {
final String streetName;
final String streetNumber;
final String city;
final String zipCode;
Place({
required this.streetName,
required this.streetNumber,
required this.city,
required this.zipCode,
});
factory Place.fromJson(Map<String, dynamic> json) => _$PlaceFromJson(json);
}
// GENERATED CODE - DO NOT MODIFY BY HAND
part of 'place_model.dart';
// **************************************************************************
// JsonSerializableGenerator
// **************************************************************************
Place _$PlaceFromJson(Map<String, dynamic> json) => Place(
streetName: json['street_name'] as String,
streetNumber: json['street_number'] as String,
city: json['city'] as String,
zipCode: json['zip_code'] as String,
);
Map<String, dynamic> _$PlaceToJson(Place instance) => <String, dynamic>{
'street_name': instance.streetName,
'street_number': instance.streetNumber,
'city': instance.city,
'zip_code': instance.zipCode,
};
我的模型需要串列中的short_name值,但目前我的模型與 json 架構不匹配。我是一個沒有經驗的開發人員,這可能很簡單。但是你能建議我一種正確生成的方法嗎?address_componentsPlaceplace_model.g.dart
uj5u.com熱心網友回復:
回應與您的目標類不匹配,因此您必須先轉換它。
為回應創建模型:
@JsonSerializable()
class Response {
final Result result;
final String status;
const Response({required this.result, required this.status});
factory Response.fromJson(Map<String, dynamic> json) =>
_$ResponseFromJson(json);
}
@JsonSerializable(fieldRename: FieldRename.snake)
class Result {
final List<AddressComponents> addressComponents;
const Result({required this.addressComponents});
factory Result.fromJson(Map<String, dynamic> json) => _$ResultFromJson(json);
}
@JsonSerializable(fieldRename: FieldRename.snake)
class AddressComponents {
final String longName;
final String shortName;
final List<String> types;
const AddressComponents({
required this.longName,
required this.shortName,
required this.types,
});
factory AddressComponents.fromJson(Map<String, dynamic> json) =>
_$AddressComponentsFromJson(json);
}
現在您可以將工廠建構式添加到 Place模型中,該模型將從以下位置構建Place模型Response:
class Place {
const Place({
this.streetName,
this.streetNumber,
this.city,
this.zipCode,
});
final String? streetName;
final String? streetNumber;
final String? city;
final String? zipCode;
factory Place.fromResponse(Response response) {
final streetNumberComponent = response.result.addressComponents
.where((c) => c.types.contains('street_number'));
// .. here the rest of Place fields
return Place(
streetNumber: streetNumberComponent.isNotEmpty
? streetNumberComponent.first.shortName
: null,
// .. here the rest of Place fields
);
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/432088.html
