I have created my model class and through Dio
I store and display the server information in my class but my problem is that I want to access the jsonarray
of chart fields json object and display them in FutureBuilder app response.
[
{
"id":"1",
"name":"arthor",
"chart":[
{
"a":"allow",
"b":"23",
"c":"256523"
},
{
"a":"allow",
"b":"23",
"c":"256522"
},
{
"a":"allow",
"b":"23",
"c":"256525"
}
]
}
]
My class model:
class Chart {
String? id;
String? name;
List<Chart>? chart;
Chart({this.id, this.name, this.chart});
Chart.fromJson(Map<String, dynamic> json) {
id = json['id'];
name = json['name'];
if (json['chart'] != null) {
chart = <Chart>[];
json['chart'].forEach((v) {
chart!.add(new Chart.fromJson(v));
});
}
}
Map<String, dynamic> toJson() {
final Map<String, dynamic> data = new Map<String, dynamic>();
data['id'] = this.id;
data['name'] = this.name;
if (this.chart != null) {
data['chart'] = this.chart!.map((v) => v.toJson()).toList();
}
return data;
}
}
class Chart {
String? a;
String? b;
String? c;
Chart({this.a, this.b, this.c});
Chart.fromJson(Map<String, dynamic> json) {
a = json['a'];
b = json['b'];
c = json['c'];
}
Map<String, dynamic> toJson() {
final Map<String, dynamic> data = new Map<String, dynamic>();
data['a'] = this.a;
data['b'] = this.b;
data['c'] = this.c;
return data;
}
static List<Chart> fromJsonList(List<dynamic> jsonlList) {
return jsonlList
.map((e) => Chart.fromJson(e as Map<String, dynamic>))
.toList();
}
}
My Future method API:
Future<List<Chart>> getCategory() async {
String link = "xxxxxxx";
var response = await Dio().get(link + id.toString());
return Rate.fromJsonList(response.data);
}
2
Answers
Artin Jan, here is an example of your need:
if it didn’t help please let me know and share more detail.
EDIT:
According to your Map :
It won’t be correct if you say
snapshot.data![index].chart!
becausesnapshot.data![0].chart!
is the only item ofsnapshot.data
and you don’t have another items. now final
chartList = snapshot.data![0].chart!
isand
If you use the recommended code with the map structure that you’ve provided, you won’t have any problem.
happy coding…