8
votes

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?

2
Another best thing to do is make your model class properties final and initialized them in you constructor, as Model(//.... obj) : value = obj["key"], //...; - Blasanka

2 Answers

4
votes

In your case Model.fromJson(Map<String, dynamic> json) is a named constructor, so inside the brackets there should be initializing done. Instead of that you're just creating a new Model inside the constructor and do nothing with it. That's why fields in your model are staying uninitialized (null by default).

It should be written like that:

Model.fromJson(Map<String, dynamic> json) {
  value1 = json['key1'];
  value2 = json['key2'];
  value3 = json['key3'];
  value4 = json['key4'];
}

Or you can use factory constructor:

factory Model.fromJson(Map<String, dynamic> json) => Model(
    value1: json['key1'],
    value2: json['key2'],
    value3: json['key3'],
    value4: json['key4'],
  );
4
votes

Try replacing your model class with below

// To parse this JSON data, do
//
//     final model = modelFromJson(jsonString);

import 'dart:convert';

List<Model> modelFromJson(String str) => List<Model>.from(json.decode(str).map((x) => Model.fromJson(x)));

String modelToJson(List<Model> data) => json.encode(List<dynamic>.from(data.map((x) => x.toJson())));

class Model {
    String key1;
    String key2;
    String key3;
    String key4;

    Model({
        this.key1,
        this.key2,
        this.key3,
        this.key4,
    });

    factory Model.fromJson(Map<String, dynamic> json) => Model(
        key1: json["key1"] == null ? null : json["key1"],
        key2: json["key2"] == null ? null : json["key2"],
        key3: json["key3"] == null ? null : json["key3"],
        key4: json["key4"] == null ? null : json["key4"],
    );

    Map<String, dynamic> toJson() => {
        "key1": key1 == null ? null : key1,
        "key2": key2 == null ? null : key2,
        "key3": key3 == null ? null : key3,
        "key4": key4 == null ? null : key4,
    };
}

and use below code to parse

List<Model> modelList = modelFromJson('Your Json String);