Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Parsing JSON in Flutter results in NULL values of objects's fields

Tags:

json

flutter

dart

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?

like image 622
Arthur Eirich Avatar asked Oct 09 '19 21:10

Arthur Eirich


People also ask

How do you deal with null values in Flutter?

Use the fallback operator to set a default value for null values before using the variable. Here, "str" is null, and we set the fallback operator with fallback value in case of "str" is null. You need to do this before using it on the code.


2 Answers

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);
like image 138
Kailash Chouhan Avatar answered Oct 03 '22 18:10

Kailash Chouhan


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'],
  );
like image 22
Kirill Bubochkin Avatar answered Oct 03 '22 20:10

Kirill Bubochkin