parsing complex json in nested object flutter I'm trying to pull the data from a list that is inside an object that is inside an object Trying to pull the type and value Suppose I want to access the visuals contains some fields and an array of objects field.
"visuals": [{"type": "color", "value": "red"},]
this my json file
{
"name": "Lionel Messi",
"email": "[email protected]",
"age": "23",
"photo": "https://img.bleacherreport.net/img/images/photos/003/738/191/hi-res-bd496f7bef33e47363703d3cce58c50e_crop_north.jpg?h=533&w=800&q=70&crop_x=center&crop_y=top",
"url": "http://www.john-wesley.com",
"trainingPlan": {
"id": "1",
"creator": "[email protected]",
"creationDate": "21/04/20",
"visuals": [
{
"type": "color",
"value": "red",
}
],
"transition": {"length": "2", "delay": "1"},
"duration": {"type": "Countdown", "time": "20"}
}
}
]
this is flutter
import 'dart:convert';
class Record {
String name;
String email;
String age;
String photo;
String url;
final trainingPlan;
Record({
this.name,
this.email,
this.age,
this.photo,
this.url,
this.trainingPlan,
});
factory Record.fromJson(Map<String, dynamic> json) {
return new Record(
name: json['name'],
email: json['email'],
age: json['age'],
photo: json['photo'],
url: json['url'],
trainingPlan: TrainingPlan.fromJson(json['trainingPlan']),
);
}
}
class TrainingPlan {
final String id;
final String creator;
final String creationDate;
final transition;
final duration;
final List<Visuals> visuals;
TrainingPlan(
{this.id,
this.creator,
this.creationDate,
this.transition,
this.duration,
this.visuals});
TrainingPlan.fromJson(Map<String, dynamic> json)
: id = json['id'],
creator = json['creator'],
creationDate = json['creationDate'],
transition = Transition.fromJson(json['transition']),
duration = Duration.fromJson(json['duration']),
visuals = parseVisuals(json);
static List<Visuals> parseVisuals(visualsJson) {
var list = visualsJson['visuals'] as List;
List<Visuals> visualsList =
list.map((data) => Visuals.fromJson(data)).toList();
return visualsList;
}
}
class Transition {
final String length;
final String delay;
Transition({
this.length,
this.delay,
});
Transition.fromJson(Map<String, dynamic> json)
: length = json['length'],
delay = json['delay'];
}
class Duration {
final String type;
final String time;
Duration({
this.type,
this.time, int seconds,
});
Duration.fromJson(Map<String, dynamic> json)
: type = json['type'],
time = json['time'];
}
class Visuals {
final String type;
final String value;
Visuals({this.type, this.value});
factory Visuals.fromJson(Map<String, dynamic> parsedJson) {
return Visuals(
type: parsedJson['type'],
value: parsedJson['value'],
}
}
parsing complex json in nested object flutter I'm trying to pull the data from a list that is inside an object that is inside an object Trying to pull the type and value Suppose I want to access the visuals contains some fields and an array of objects field.
package:built_valueorpackage:json_serializablefor dealing with JSON. Usingdart:convertin a robust way that can gracefully fail on malformed input is a lot of work. See flutter.dev/docs/development/data-and-backend/json