I am trying to convert JSON response to dart but over 3 error. I convert the Json from online website https://javiercbk.github.io/json_to_dart/
the errors says : The method 'toJson' isn't defined for the type 'List'. Try correcting the name to the name of an existing method, or defining a method named 'toJson'.
the code :
class RedZoneModel {
bool? status;
List<Data>? data;
RedZoneModel({this.status, this.data});
RedZoneModel.fromJson(Map<String, dynamic> json) {
status = json['status'];
if (json['data'] != null) {
data = <Data>[];
json['data'].forEach((v) { data!.add(new Data.fromJson(v)); });
}
}
Map<String, dynamic> toJson() {
final Map<String, dynamic> data = new Map<String, dynamic>();
data['status'] = this.status;
if (this.data != null) {
data['data'] = this.data!.map((v) => v.toJson()).toList();
}
return data;
}
}
class Data {
int? id;
String? areaName;
Geojson? geojson;
String? createdAt;
String? updatedAt;
Data({this.id, this.areaName, this.geojson, this.createdAt, this.updatedAt});
Data.fromJson(Map<String, dynamic> json) {
id = json['id'];
areaName = json['area_name'];
geojson = json['geojson'] != null ? new Geojson.fromJson(json['geojson']) : null;
createdAt = json['created_at'];
updatedAt = json['updated_at'];
}
Map<String, dynamic> toJson() {
final Map<String, dynamic> data = new Map<String, dynamic>();
data['id'] = this.id;
data['area_name'] = this.areaName;
if (this.geojson != null) {
data['geojson'] = this.geojson!.toJson();
}
data['created_at'] = this.createdAt;
data['updated_at'] = this.updatedAt;
return data;
}
}
class Geojson {
String? type;
List<List>? coordinates;
Geojson({this.type, this.coordinates});
Geojson.fromJson(Map<String, dynamic> json) {
type = json['type'];
if (json['coordinates'] != null) {
coordinates = <List>[];
json['coordinates'].forEach((v) { coordinates!.add(new List.fromJson(v)); }); // ! Error at List.fromJson(v)
}
}
Map<String, dynamic> toJson() {
final Map<String, dynamic> data = new Map<String, dynamic>();
data['type'] = this.type;
if (this.coordinates != null) {
data['coordinates'] = this.coordinates!.map((v) => v.toJson()).toList(); // ! Error at v.toJson()
}
return data;
}
}
class Coordinates {
Coordinates({ }); // ! Error at Coordinates({ });
Coordinates.fromJson(Map<String, dynamic> json) {
}
Map<String, dynamic> toJson() {
final Map<String, dynamic> data = new Map<String, dynamic>();
return data;
}
}
and this is the response :
{
"status": true,
"data": [
{
"id": 8,
"area_name": "Surian",
"geojson": {
"type": "Polygon",
"coordinates": [
[
[
-1.2453052,
460.8429751
],
[
-1.2200768,
460.9066456
],
[
-1.2564606,
460.9423423
],
[
-1.2940451,
460.8711205
],
[
-1.2453052,
460.8429751
],
[
-1.2940451,
460.8711205
],
[
-1.2453052,
460.8429751
]
]
]
},
"created_at": "2022-08-29T14:30:10.000000Z",
"updated_at": "2022-12-19T18:30:10.000000Z"
},
{
"id": 13,
"area_name": "test edit",
"geojson": {
"type": "Polygon",
"coordinates": [
[
[
123,
123
],
[
123,
123
],
[
123,
123
],
[
123,
123
],
[
123,
123
]
]
]
},
"created_at": "2022-08-29T15:43:35.000000Z",
"updated_at": "2022-12-20T08:28:00.000000Z"
}
]
}
Anyone have any suggestion?
CodePudding user response:
To convert JSON to a Dart object, you can use the json_serializable package. This package allows you to generate code that can parse JSON into a Dart object and serialize a Dart object into JSON.
First, you will need to add the json_serializable package to your pubspec.yaml file:
dependencies:
json_serializable: ^3.6.0
Next, you will need to create a Dart class that represents the structure of your JSON data. This class should have the same structure as your JSON data, with each field in the class corresponding to a field in the JSON.
For example, if you have the following JSON data:
{
"name": "John Smith",
"age": 30
}
You can create a Dart class like this:
import 'package:json_annotation/json_annotation.dart';
part 'user.g.dart';
@JsonSerializable()
class User {
final String name;
final int age;
User(this.name, this.age);
factory User.fromJson(Map<String, dynamic> json) => _$UserFromJson(json);
Map<String, dynamic> toJson() => _$UserToJson(this);
}
Next, you will need to generate the JSON serialization code by running the following command:
Copy code flutter pub run build_runner build This will generate the user.g.dart file, which contains the code needed to serialize and deserialize your Dart object.
Finally, you can use the fromJson factory method to parse JSON into a User object, and the toJson method to serialize a User object into JSON.
import 'package:my_app/user.dart';
void main() {
// Parse JSON into a User object
var json = '{"name": "John Smith", "age": 30}';
var user = User.fromJson(jsonDecode(json));
// Serialize a User object into JSON
var userJson = jsonEncode(user.toJson());
}
CodePudding user response:
You shouldn't really use online generators that convert JSON to Dart objects. Instead, learn about json and serialization. You can use dart packages such as freezed and json_serializable to generate the encoding boilerplate for your JSON response.
Here is sample json to Dart object converter for your json response above:
class Data {
const Data({
required this.id,
required this.area_name,
required this.geojson,
required this.created_at,
required this.updated_at,
});
factory Data.fromJson(Map<String, dynamic> json) => Data(
id: json['id'] as int,
area_name: json['area_name'] as String,
geojson: GeoJson.fromJson(json['geojson'] as Map<String, dynamic>),
created_at: DateTime.parse(json['created_at'] as String),
updated_at: DateTime.parse(json['updated_at'] as String),
);
final int id;
final String area_name;
final GeoJson geojson;
final DateTime created_at;
final DateTime updated_at;
}
class GeoJson {
const GeoJson({
required this.type,
required this.coordinates,
});
factory GeoJson.fromJson(Map<String, dynamic> json) => GeoJson(
type: json['type'] as String,
coordinates: (json['coordinates'] as List<dynamic>)
.map((e) => Polygon.fromJson(e as Map<String, dynamic>))
.toList(),
);
final String type;
final List<Polygon> coordinates;
}
class Polygon {
const Polygon({
required this.lat,
required this.lang,
});
/// Creates a Polygon from Json map
factory Polygon.fromJson(Map<String, dynamic> json) => Polygon(
lat: json['lat'] as num,
lang: json['lang'] as num,
);
final num lat;
final num lang;
}