I have a model like:
class PotholeData {
List<Coordinates> coordinates;
String location;
String image;
PotholeData({this.coordinates, this.location, this.image});
PotholeData.fromJson(Map<String, dynamic> json) {
if (json['coordinates'] != null) {
coordinates = new List<Coordinates>();
json['coordinates'].forEach((v) {
coordinates.add(new Coordinates.fromJson(v));
});
}
location = json['location'];
image = json['image'];
}
Map<String, dynamic> toJson()
{
final Map<String, dynamic> data = new Map<String, dynamic>();
if (this.coordinates != null) {
data['coordinates'] = this.coordinates.map((v) => v.toJson()).toList(); }
data['location'] = this.location;
data['image'] = this.image;
return data;
}
}
class Coordinates {
String x;
String y;
String w;
String h;
Coordinates({this.x, this.y, this.w, this.h});
Coordinates.fromJson(Map<String, dynamic> json) {
x = json['x'];
y = json['y'];
w = json['w'];
h = json['h'];
}
Map<String, dynamic> toJson() {
final Map<String, dynamic> data = new Map<String, dynamic>();
data['x'] = this.x;
data['y'] = this.y;
data['w'] = this.w;
data['h'] = this.h;
return data;
}
}
I am trying to put this data into Firebase and the way I am doing is:
Map<String, dynamic> potholeData = PotholeData(
coordinates: sampleCoordinates,
image: "File name",
location: "Live Location").toJson();
obj.addData(potholeData).catchError((e) {
print(e);
});
}
where the sampleCoordinates is a list of type coordinates. But I am not getting what form of data should be in it. I was trying out with hardcoded data in it but every time I put anything an error pops up stating the element of type List/Map/String/int can't be assigned to the list type Coordinates.
The sample JSON data looks like:
{
"coordinates": [
{
"x": "x_coor",
"y": "y_coor",
"w": "width",
"h": "heigth"
},
{
"x": "x_coor",
"y": "y_coor",
"w": "width",
"h": "heigth"
}
],
"location": "location",
"image": "image"
}
I need help in understanding what kind of data should be inside sampleCoordinates. Should it be a Map/List/String/int? sampleCoordinates is hardcoded for your information.
I had tried putting some data like below but none of them worked. Technically, the first one should have worked. The following was tried:
List<Coordinates> sampleCoordinates = [{
"x": "x_coor",
"y": "y_coor",
"w": "width",
"h": "heigth"
},
{
"x": "x_coor",
"y": "y_coor",
"w": "width",
"h": "heigth"
}];
OR
List<Coordinates> sampleCoordinates = [123,1234];
OR
List<Coordinates> sampleCoordinates = ["asb","adgad"];