我有一個 Google 地圖 API,其中我試圖在 SQFLite 中保存該特定位置的位置鍵和 JSON 回應。我能夠撰寫 fromJSON 方法,但如何撰寫 toMap 方法?
這是沒有 toMap 方法的模型
class GeometryModel {
final LocationModel locationModel;
GeometryModel({required this.locationModel});
factory GeometryModel.fromJson(Map<dynamic, dynamic> parsedJson) {
return GeometryModel(locationModel: LocationModel.fromJson(parsedJson['location']));
}
}
class LocationModel {
final double latitude;
final double longitude;
LocationModel({required this.latitude, required this.longitude});
factory LocationModel.fromJson(Map<dynamic, dynamic> parsedJson) {
return LocationModel(latitude: parsedJson['lat'], longitude: parsedJson['lng']);
}
}
class PlaceModel {
final String placeId;
final GeometryModel geometryModel;
final String address;
final String name;
PlaceModel({required this.placeId, required this.geometryModel, required this.address, required this.name});
factory PlaceModel.fromJson(Map<String, dynamic> parsedJson) {
return PlaceModel(
placeId: parsedJson['place_id'],
name: parsedJson['vicinity'],
geometryModel: GeometryModel.fromJson(parsedJson['geometry']),
address: parsedJson['formatted_address'],
);
}
}
這是我從服務器收到的回應
{
"html_attributions": [],
"result": {
"formatted_address": "New York, NY, USA",
"geometry": {
"location": {
"lat": 40.7127753,
"lng": -74.0059728
},
"viewport": {
"northeast": {
"lat": 40.91757705070789,
"lng": -73.70027206817629
},
"southwest": {
"lat": 40.47739906045452,
"lng": -74.25908991427882
}
}
},
"icon_background_color": "#7B9EB0",
"icon_mask_base_uri": "https://maps.gstatic.com/mapfiles/place_api/icons/v2/generic_pinlet",
"name": "New York"
"url": "https://maps.google.com/?q=New York, NY, USA&ftid=0x89c24fa5d33f083b:0xc80b8f06e177fe62",
"utc_offset": -240,
"vicinity": "New York","
},
"status": "OK"
}
uj5u.com熱心網友回復:
使用JsonToDart網站。您只需要粘貼 JSON 資料,它就會將 json 資料與fromJson()andtoJson()或一起轉換為模型類toMap()。
注意:如果您收到 json 語法錯誤,您需要驗證您的 json 資料是否有效。對于 json 驗證檢查json_parser_online網站。
uj5u.com熱心網友回復:
還有另一個選項可以處理 JSON 資料,無需創建模型,也無需使用生成器!如何?使用g-json 包。
Naan Avan,請參閱我對另一個問題的回答(如何使用 Flutter 中的默認方法決議復雜的 JSON?)。
uj5u.com熱心網友回復:
這是示例Viewport,如何轉換 toJson/toMap 并使用此鏈接幫助您將 JSON 轉換為 map
class Viewport {
Viewport viewport;
Viewport({this.viewport});
Viewport.fromJson(Map<String, dynamic> json) {
viewport = json['viewport'] != null
? new Viewport.fromJson(json['viewport'])
: null;
}
Map<String, dynamic> toJson() {
final Map<String, dynamic> data = new Map<String, dynamic>();
if (this.viewport != null) {
data['viewport'] = this.viewport.toJson();
}
return data;
}
}
class Viewport {
Northeast northeast;
Northeast southwest;
Viewport({this.northeast, this.southwest});
Viewport.fromJson(Map<String, dynamic> json) {
northeast = json['northeast'] != null
? new Northeast.fromJson(json['northeast'])
: null;
southwest = json['southwest'] != null
? new Northeast.fromJson(json['southwest'])
: null;
}
Map<String, dynamic> toJson() {
final Map<String, dynamic> data = new Map<String, dynamic>();
if (this.northeast != null) {
data['northeast'] = this.northeast.toJson();
}
if (this.southwest != null) {
data['southwest'] = this.southwest.toJson();
}
return data;
}
}
class Northeast {
double lat;
double lng;
Northeast({this.lat, this.lng});
Northeast.fromJson(Map<String, dynamic> json) {
lat = json['lat'];
lng = json['lng'];
}
Map<String, dynamic> toJson() {
final Map<String, dynamic> data = new Map<String, dynamic>();
data['lat'] = this.lat;
data['lng'] = this.lng;
return data;
}
}
uj5u.com熱心網友回復:
你可以這樣實作:
Map<String, dynamic> toMap() {
var map = Map<String, dynamic>();
map['place_id'] = placeId;
map['address'] = address;
map['name'] = name;
map['geometry_model_id']=geometryModel.id; //saves id
return map;}
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/313941.html
上一篇:WebGrid.getHtml問題。對型別“IHtmlString”的參考聲稱它在“System.Web”中定義,但找不到
