Home > Mobile >  How to write the model for the JOSN ? Flutter
How to write the model for the JOSN ? Flutter

Time:10-05

I have a Google maps API where in i was trying to save the place key and the JSON response of that particular place in the SQFLite. I was able to write the fromJSON method but how to write the toMap method?

This is the model without the toMap method

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'],
    );
  }
}


This is the response I'm receiving from the server

{
    "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"
}

CodePudding user response:

Here is the example of Viewport, how to convert toJson/toMap and use this link to help you to convert your JSON to 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;
  }
}

CodePudding user response:

You can achieve it like this :

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

CodePudding user response:

Use JsonToDart website. You just need to paste your JSON data and it will convert json data to model class along with fromJson() and toJson() or toMap().

NOTE: If you get json syntax error you need to verify your json data is Valid or not. For json validation check json_parser_online website.

CodePudding user response:

There's another option to work with JSON data and without creating models and without using generators! How? Using the g-json package.

See my answer to another question (How to parse complex JSON using default methods in Flutter?) that can help you, Naan Avan.

  • Related