Serialize JSON List in Flutter

Asked

Viewed 843 times

1

I have an application in Fluter, and I have a return Json with List. I wanted to serialize the List to display it at a certain point, but I couldn’t find a way to do that. An example of my code:

class ResultLogin {
 final String token;
 final List<ZLoginResultSchema>schemas;


  ResultLogin({this.token, this.schemas});

  ResultLogin.fromJson(Map<String, dynamic> json):
    token      = json['token'],
    schemas    = new List.from( json['schemas']);


Map<String, dynamic> toJson() =>
  {
    'token':    token,
    'schemas':  schemas,
  };
}

My Class Zloginresultschema:

class ZLoginResultSchema  {
 final String name;
 final String fullname;


 ZLoginResultSchema({this.name, this.fullname});


 ZLoginResultSchema.fromJson(Map<String, dynamic> json):
    name    = json['name'],
    fullname = json['fullname'];


 Map<String, dynamic> toJson() =>
  {
    'name': name,
    'fullname': fullname,
  };
 }

In the ResultLogin.fromJson token works, but the schema list does not, how to serialize a Json list?

2 answers

1


Note that you are trying to have Dart serialize/deserialize a list with a custom object ZLoginResultSchema, without the language having the slightest idea of how to do this, after all this is a custom object that you created. If it was a list of String, would even work.

You have to use the class fromJson method ZLoginResultSchemafor Dart to understand how it should pass json to an object. So the class ResultLogin would be:

class ResultLogin {
  final String token;
  final List<ZLoginResultSchema> schemas;

  ResultLogin({this.token, this.schemas});

  factory ResultLogin.fromJson(Map<String, dynamic> json) {
    var list = json['schemas'] as List;
    List<ZLoginResultSchema> schemas =
        list.map((z) => ZLoginResultSchema.fromJson(z));

    return ResultLogin(token: json['token'], schemas: schemas);
  }

  Map<String, dynamic> toJson() => {
        'token': token,
        'schemas': schemas,
      };
}

Now note in the fromJson method the need to have to transform the Map from json to a List and convert one by one with the ZLoginResultSchema.fromJson(z). It’s not practical at all, right?

That is why we do not recommend doing this manual json transformation as you are doing. For this there are tools like lib json_serializable using the build_runner to generate these json auto-conversion codes, in addition to having other various features that make our lives much easier.

I recommend you use it, for more details.

1

Your object can look like this:

class ResultLogin {
 final String token;
 final List<ZLoginResultSchema>schemas;

  ResultLogin({this.token, this.schemas});

  ResultLogin.fromJson(Map<String, dynamic> json) {
    token = json['token'],
    if (json['schemas'] != null) {
     this.schemas = List<ZLoginResultSchema>();
     json['schemas'].forEach((obj) {
      schemas.add(ZLoginResultSchema.fromJson(obj));
     });
    }
  }

  Map<String, dynamic> toJson() {
   final Map<String, dynamic> data = new Map<String, dynamic>();
   data['token'] = this.token;
   data['schemas'] = this.schemas.toJson();
   return data;
  }
}

By using Foreach we will be taking each item from the list and converting your Map to an object and then adding it to the list. If you have the structure of JSON, preferably having it filled use the JSON to Dart.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.