1

They are not found in the same dart JSON:

{
    "museum1" : {
        "info1" : "value1",
        "info2" : "value1"
    },
    "museum2" : {
        "info1" : "value2",
        "info2" : "value2"
    }
    ...
}

museum class:

class museo{
    String info1;       
    String info2
    
    museo({this.info1, this.info2});
}

Is there a way to retrieve info1 and info2 by knowing the id? I would like to create museum object and just do museum.info1

  • Just decode the JSON and you can easily access its data. Please refer to this https://stackoverflow.com/a/15867374/9455325 – rickimaru Jan 29 '21 at 10:30

1 Answers1

0

If you have a simple entity you can define a factory constructor for create object from JSON data:

class museo{
  String info1;       
  String info2
    
  museo({this.info1, this.info2});

  factory museo.fromJson(Map<String, dynamic> data) {
    return museo(
      info1: data['info1'],
      info2: data['info2'],
    );
  }
}

And after JSON parsing extract objects:

final museos = parsedJson.map(museo.fromJson); // it should be List<museo>

But if your object large and complex in parsing, you can try to use plugins like json_serializable.

All JSON cast variants described in Flutter documentation.

fartem
  • 2,361
  • 2
  • 8
  • 20