0

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"];
2
  • Does this answer your question? Create Json Arrays and List with SharedPreferences Commented Mar 1, 2020 at 14:57
  • @Kahou No, it doesn't. That question is purely for SharedPrefs and it doesn't show how to create nested JSON data of a specific type! Commented Mar 1, 2020 at 15:03

2 Answers 2

1

The user class with JSON convert: JSON and serialization

class User {
  String name;
  int age;
  User father;

  User({
    this.name,
    this.age,
    this.father,
  });

  factory User.fromJson(String str) => User.fromMap(json.decode(str));

  String toJson() => json.encode(toMap());

  factory User.fromMap(Map<String, dynamic> json) => User(
        name: json["name"],
        age: json["age"],
        father: json["father"] == null ? null : User.fromMap(json["father"]),
      );

  Map<String, dynamic> toMap() => {
        "name": name,
        "age": age,
        "father": father == null ? null : father.toMap(),
      };
}

Sign up to request clarification or add additional context in comments.

1 Comment

I don't want to serialize my data. I edited my question after your comment. I want to create JSON data which has a List of a specific type i.e. Coordinates in this case. Check my answer which I have posted. You will understand what I wanted!! Cheers for trying to help me out
0

So, I found the answer while trying out many things. The sampleCoordinates should have:

List<Coordinates> sampleCoordinates = [
    Coordinates(h: '24',w: '3455', x: '34', y: '345'), 
    Coordinates(h: '24',w: '3455', x: '34', y: '345')
];

This served my purpose.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.