0

I have a following problem. I just cannot understand what can be the easiest way to parse an information from JSON to an array of objects in flutter.

I have a following class:

class Organization {
  String subject;
  String organization;
  String shortDescription;
  String address;
  String phoneNumber;
  String contactInfo;
  List<String> tags;

  Organization({
    this.subject,
    this.organization,
    this.shortDescription,
    this.address,
    this.phoneNumber,
    this.contactInfo,
    this.tags,
  });

  Organization.fromJson(Map<String, dynamic> json) {
    subject = json['Subject'];
    organization = json['Organization'];
    shortDescription = json['Short description'];
    address = json['Address'];
    phoneNumber = json['Phone number'];
    contactInfo = json['Contact info'];
    tags = json['Tags'].cast<String>();
  }

  Map<String, dynamic> toJson() {
    final Map<String, dynamic> data = new Map<String, dynamic>();
    data['Subject'] = this.subject;
    data['Organization'] = this.organization;
    data['Short description'] = this.shortDescription;
    data['Address'] = this.address;
    data['Phone number'] = this.phoneNumber;
    data['Contact info'] = this.contactInfo;
    data['Tags'] = this.tags;
    return data;
  }
}

This is the small sample of Json file that I have in assets folder.

[
  {
    "Subject": "Cultuur - Culture ",
    "Organization": "Museum aan de stroom ",
    "Short description": "The MAS is a museum that provides you with permanent and temporary exhibitions. These offer a refreshing look at the city and it's history. At the top, on the tenth floor, you can enjoy a 360-degree panorama of the city, the port and the river.",
    "Address": "Hanzestedenplaats 1, 2000 Antwerpen",
    "Phone number": "+32 3 338 44 00",
    "Contact info": "www.mas.be",
    "Tags": ["Meeting place", "Museum", "Antwerp", "Art"]
  },
  {
    "Subject": "Cultuur - Culture ",
    "Organization": "Red star line museum ",
    "Short description": "Through personal story's of immigrants the Red star line museum paints a picture of the immigration history in Antwerp. ",
    "Address": "Montevideostraat 3, 2000 Antwerpen",
    "Phone number": "+32 3 298 27 70",
    "Contact info": "www.redstarline.be",
    "Tags": ["Museum", "Antwerp", "History", "Immigration"]
  }
]

I am just trying to parse information from this file to a list of Organisations as presented below:

List<Organisation> organisations = [];

  Future<void> readJson() async {
    final String response =
        await rootBundle.loadString('assets/organisations.json');
    final data = await json.decode(response);
    organisations = data;
  }

However I get Unhandled Exception: type List dynamic is not a subtype of type List Organisation error.

In my opinion data should be an array of my objects. What am I doing wrong? Or maybe is there simpler method to achieve this result?

Michał Śniady
  • 119
  • 3
  • 14

1 Answers1

0

At the final of readJson función, you can call Organization.fromJson like this:

Future<void> _readJson() async {
    final String response =
        await rootBundle.loadString('assets/data/organizations.json');
    final List<dynamic> data = await json.decode(response);

    for (dynamic it in data) {
      final Organization organization = Organization.fromJson(it); // Parse data    
      organizations.add(organization); // and organization to List
    }
  }
Jonathan Ixcayau
  • 567
  • 3
  • 14