I have a JSON file in the following structure:
[
{
"key1":"value1",
"key2":"value2",
"key3":"value3",
"key4":"value4",
},
{
"key1":"value1",
"key2":"value2",
"key3":"value3",
"key4":"value4",
}
]
which I get over an HTTP call. I try to parse this JSON into flutter objects. Therefore I wrote a class:
class Foo {
List<Model> modelsAsJson;
Foo({this.modelsAsJson});
Foo.fromJson(List<dynamic> jsonData) {
modelsAsJson = jsonData.map((listItem) => Model.fromJson(listItem)).toList();
Foo(modelsAsJson: modelsAsJson);
}
}
And I also wrote another class for Model:
class Model {
String value1;
String value2;
String value3;
String value4;
Model({this.value1, this.value2, this.value3, this.value4});
Model.fromJson(Map<String, dynamic> json) {
Model(value1: json['key1'], value2: json['key2'], value3: json['key3'], value4: json['key4'],);
}
Map<String, dynamic> toJson() => {
'key1': value1,
'key2': value2,
'key3': value3,
'key4': value4
};
}
After getting the data over HTTP I parse it into the given objects like that: http.Response = http.get(urlHere); final decodedJson = jsonDecode(response.body); result = Foo.fromJson(decodedJson).modelsAsJson;
After parsing the result gets the correct length from modelsAsJson but every model in that list hast value1..value4 being nulls. What am I doing wrong?
Model(//.... obj) : value = obj["key"], //...;- Blasanka