Thomas Nicole
Thomas Nicole

Reputation: 785

Flutter - What is the best way to parse Json?

Actually, I am using the traditional way to work with Json:

factory MyObject.fromJson(Map<String, dynamic> json)

I have a lot of objects dealing with Json and over time, I encounter problems like:

Converting object to an encodable object failed: Instance of 'MyObject'#0

I am looking for the best way (external plugin or something else) to manipulate these Json.

Upvotes: 0

Views: 51

Answers (2)

Alex Rabin
Alex Rabin

Reputation: 422

This is how I would set up the MyObject class to parse Json

class MyObject {
    String value;


    MyObject({this.value});

    static MyObject fromMap(Map<String,dynamic> map){
      var value = map['value'];

      return MyObject(value:value);
    }
}

Upvotes: 1

Rubens Melo
Rubens Melo

Reputation: 3315

Take a look on json_serializable package.

And docs has an excellent resource about JSON serialization.

Upvotes: 1

Related Questions