Parse JSON to Dart with nested List objects - json

How to parse from JSON to Dart object including 2 nested classes inside of properties oof the class, Lists?
A JSON object Video that contains a list of Question objects.
Question objects contains a list of Ans objects.
{
"id": 1,
"videoUrl": "https://assets.mixkit.co/videos/preview/mixkit-machinery-of-a-very-close-watch-3673-large.mp4",
"questions": [
{
"id": "0-903669-72-2",
"type": "mc",
"body": "Understand reality truth food agency artist.",
"correctAnswerId": "0-259-85157-4",
"ans": [
{
"id": "0-259-85157-4",
"body": "Play."
},
{
"id": "0-694-71578-6",
"body": "Whose trip."
},
{
"id": "0-13-124278-4",
"body": "Of figure why."
},
{
"id": "0-8169-6726-1",
"body": "Station culture green."
}
]
},
{
"id": "1-872297-31-5",
"type": "mc",
"body": "Especially resource benefit beautiful world six.",
"correctAnswerId": "1-61799-113-9",
"ans": [
{
"id": "0-384-69655-4",
"body": "Form."
},
{
"id": "0-89336-975-6",
"body": "Call."
},
{
"id": "1-61799-113-9",
"body": "Money three young."
},
{
"id": "1-60950-585-9",
"body": "Three threat back."
}
]
},
{
"id": "0-297-13339-X",
"type": "mc",
"body": "Of smile coach second firm ahead.",
"correctAnswerId": "1-916803-19-9",
"ans": [
{
"id": "0-15-955520-5",
"body": "Add old catch."
},
{
"id": "0-606-65499-2",
"body": "Well great task."
},
{
"id": "0-7364-1942-X",
"body": "Arrive resource speech kid."
},
{
"id": "1-916803-19-9",
"body": "Reach brother book."
}
]
},
{
"id": "0-254-52906-2",
"type": "ms",
"correctAnswers": [
"1-146-90255-7",
"0-17-470673-1"
],
"body": "Particularly affect but necessary.",
"ans": [
{
"id": "0-17-278557-X",
"body": "Response bill."
},
{
"id": "0-17-470673-1",
"body": "Attack sister interview."
},
{
"id": "0-16-027096-0",
"body": "Design garden."
},
{
"id": "1-146-90255-7",
"body": "Short break."
}
]
},
{
"id": "0-926285-49-1",
"type": "ms",
"correctAnswers": [
"0-554-50421-9",
"0-294-02768-8"
],
"body": "Experience family training.",
"ans": [
{
"id": "0-8260-5153-7",
"body": "Mouth exist kid."
},
{
"id": "0-294-02768-8",
"body": "Agreement factor."
},
{
"id": "0-554-50421-9",
"body": "Down race professional show."
},
{
"id": "1-124-45547-7",
"body": "Most such onto strategy."
}
]
}
]
}
I tried using this tool after failing to properly do this myself.
https://javiercbk.github.io/json_to_dart/
This is the class definitions I got(I changed some things because of VSCode suggestions).
class Video {
int? id;
String? videoUrl;
List<Question>? questions;
Video({this.id, this.videoUrl, this.questions});
Video.fromJson(Map<String, dynamic> json) {
id = json['id'];
videoUrl = json['videoUrl'];
if (json['questions'] != null) {
questions = <Question>[];
json['questions'].forEach((v) {
questions!.add(Question.fromJson(v));
});
}
}
Map<String, dynamic> toJson() {
final Map<String, dynamic> data = <String, dynamic>{};
data['id'] = id;
data['videoUrl'] = videoUrl;
if (this.questions != null) {
data['questions'] = questions!.map((v) => v.toJson()).toList();
}
return data;
}
}
class Question {
String? id;
String? type;
String? body;
String? correctAnswerId;
List<Ans>? ans;
List<String>? correctAnswers;
Question(
{this.id,
this.type,
this.body,
this.correctAnswerId,
this.ans,
this.correctAnswers});
Question.fromJson(Map<String, dynamic> json) {
id = json['id'];
type = json['type'];
body = json['body'];
correctAnswerId = json['correctAnswerId'];
if (json['ans'] != null) {
ans = <Ans>[];
json['ans'].forEach((v) {
ans!.add(Ans.fromJson(v));
});
}
correctAnswers = json['correctAnswers'].cast<String>();
}
Map<String, dynamic> toJson() {
final Map<String, dynamic> data = <String, dynamic>{};
data['id'] = id;
data['type'] = type;
data['body'] = body;
data['correctAnswerId'] = correctAnswerId;
if (ans != null) {
data['ans'] = ans!.map((v) => v.toJson()).toList();
}
data['correctAnswers'] = correctAnswers;
return data;
}
}
class Ans {
String? id;
String? body;
Ans({this.id, this.body});
Ans.fromJson(Map<String, dynamic> json) {
id = json['id'];
body = json['body'];
}
Map<String, dynamic> toJson() {
final Map<String, dynamic> data = <String, dynamic>{};
data['id'] = id;
data['body'] = body;
return data;
}
}
I load the JSON like this.
getVid() async {
final String response = await rootBundle.loadString('assets/videos.json');
final data = await json.decode(response)['videos'];
List<Video> vids = List<Video>.from(data.map((x) => Video.fromJson(x)));
}
But end up with this error message inside of the console.
Uncaught (in promise) Error: NoSuchMethodError: 'cast'
Dynamic call of null.
Receiver: null
Arguments: []
Your help is greatly appreciated!

This line of code correctAnswers = json['correctAnswers'].cast<String>(); is causing error, as it is not able to cast it as list of string
Try using List.from() or map() or List.of()methods
correctAnswers = List<String>.from(json['correctAnswers']);
Or
correctAnswers = json['correctAnswers'].map((answer) => answer.toString()).toList();
Or
correctAnswers = List<String>.of(json['correctAnswers']);

Related

Put JSON Data in Flutter Stacked Chart

I have try to put my JSON data in flutter Stacked Chart.
I already work on simple charts using JSON Data like bar, column, pie, Doughnut charts etc.
I have refer
stacked-column-chart(syncfusion_flutter_charts),
Grouped Bar Chart(charts_flutter)
Stack Overflow Que-Ans
below like my API response/JSON String
[{
"name": "ABC",
"subject": [{
"name": "Math",
"marks": "54"
},
{
"name": "Physics",
"marks": "65"
}
]
},
{
"name": "PQR",
"subject": [{
"name": "Chemistry",
"marks": "53"
},
{
"name": "Biology",
"marks": "22"
},
{
"name": "English",
"marks": "7 "
},
{
"name": "Math",
"marks": "12"
}
]
}, {
"name": "JKL",
"subject": [{
"name": "Chemistry",
"marks": "53"
},
{
"name": "Biology",
"marks": "22"
},
{
"name": "English",
"marks": "79 "
},
{
"name": "Math",
"marks": "12"
},
{
"name": "Physics",
"marks": "72"
}
]
}
]
Or I want below type of graph using JSON Data
Note: Suggest me my JSON string is wrong, you can create your own JSON data and display the output
Using charts_flutter. Please customize it for your usecase its a bare minimum implementation to validate that its working for your json.
import 'package:flutter/material.dart';
import 'dart:math';
import 'package:flutter/material.dart';
import 'package:charts_flutter/flutter.dart' as charts;
import 'dart:convert';
class StackedBarChart extends StatelessWidget {
final bool animate;
StackedBarChart({this.animate = false});
// EXCLUDE_FROM_GALLERY_DOCS_END
#override
Widget build(BuildContext context) {
String jsonString = '[{"name":"ABC","subject":[{"name":"Math","marks":"54"},{"name":"Physics","marks":"65"}]},{"name":"PQR","subject":[{"name":"Chemistry","marks":"53"},{"name":"Biology","marks":"22"},{"name":"English","marks":"7 "},{"name":"Math","marks":"12"}]},{"name":"JKL","subject":[{"name":"Chemistry","marks":"53"},{"name":"Biology","marks":"22"},{"name":"English","marks":"79 "},{"name":"Math","marks":"12"},{"name":"Physics","marks":"72"}]}]';
final studentMarks = studentMarksFromJson(jsonString);
var subjects = <Subject?>{};
var subjectsDist = <Subject?>{};
int c=0;
for (var stdnt in studentMarks) {
for (var subjs in stdnt.subject) {
if (!subjectsDist.where((element) => element?.name==subjs.name).isNotEmpty) {
subjs.sno=c++;
subjectsDist.add(subjs);
}
}
}
print(subjectsDist.length);
List<List<OrdinalMarks>> SubjectData = [];
for (var subjs in subjectsDist) {
List<OrdinalMarks> marksData = [];
for (var stdnt in studentMarks) {
if (stdnt.subject
.where((element) => element.name == subjs?.name).isNotEmpty) {
var temp = stdnt.subject
.where((element) => element.name == subjs?.name)
.first;
marksData.add(OrdinalMarks(temp.name, int.parse(temp.marks),stdnt.name));
} else {
marksData.add(OrdinalMarks(subjs!.name, 0,stdnt.name));
}
}
SubjectData.add(marksData);
}
var palettes = charts.MaterialPalette.getOrderedPalettes(subjectsDist.length+2);
int cnt=0;
List<charts.Series<OrdinalMarks, String>> chartData = [
];
for(var d in SubjectData)
{
chartData.add(new charts.Series<OrdinalMarks, String>(
id: d.first.subjectName,
domainFn: (OrdinalMarks m, _) => m.studentName,
measureFn: (OrdinalMarks m, _) => m.marks,
data: d,
fillColorFn: ( subj, _) {
// print(subj.subjectName+": subj.subjectName :" + pallets[subj.subjectName].toString()??charts.MaterialPalette.blue.shadeDefault.toString());
return palettes.elementAt( subjectsDist.where((element) => element?.name==subj.subjectName).first?.sno??0 ).shadeDefault; //pallets[subj.subjectName]??charts.MaterialPalette.blue.shadeDefault;
},
colorFn: ( subj, _) {
// print(subj.subjectName+": subj.subjectName :" + pallets[subj.subjectName].toString()??charts.MaterialPalette.blue.shadeDefault.toString());
return palettes.elementAt(subjectsDist.where((element) => element?.name==subj.subjectName).first?.sno??0).shadeDefault;
},
));
}
return Scaffold(
// Use Obx(()=> to update Text() whenever count is changed.
appBar: AppBar(title: Text("Chart")),
// Replace the 8 lines Navigator.push by a simple Get.to(). You don't need context
body:new charts.BarChart(
chartData,
animate: animate,
behaviors: [new charts.SeriesLegend(showMeasures: true)],
animationDuration: Duration(seconds: 3),
));
}
}
/// Sample ordinal data type.
class OrdinalMarks {
final String subjectName;
final int marks;
final String studentName;
OrdinalMarks(this.subjectName, this.marks,this.studentName);
}
List<StudentMarks> studentMarksFromJson(String str) => List<StudentMarks>.from(json.decode(str).map((x) => StudentMarks.fromJson(x)));
String studentMarksToJson(List<StudentMarks> data) => json.encode(List<dynamic>.from(data.map((x) => x.toJson())));
class StudentMarks {
StudentMarks({
required this.name,
required this.subject,
});
String name;
List<Subject> subject;
factory StudentMarks.fromJson(Map<String, dynamic> json) => StudentMarks(
name: json["name"],
subject: List<Subject>.from(json["subject"].map((x) => Subject.fromJson(x))),
);
Map<String, dynamic> toJson() => {
"name": name,
"subject": List<dynamic>.from(subject.map((x) => x.toJson())),
};
}
class Subject {
Subject({
required this.name,
required this.marks,
});
String name;
String marks;
int? sno;
factory Subject.fromJson(Map<String, dynamic> json) => Subject(
name: json["name"],
marks: json["marks"],
);
Map<String, dynamic> toJson() => {
"name": name,
"marks": marks,
};
}

error when trying to decode json and casting it

I am trying to decode complex json data and always resulting in error.
Below are my json data:
{
"status_code": 200,
"message": "Successfully get data user",
"data": {
"sec_user": {
"clientIp": "1.4.2.54",
"officeid": "N/A",
},
"username": "uname",
"name1": "",
"menus": [
{
"appl_id": "MTC",
"menu_caption": "Master",
"menu_path": "/master",
"submenus": [
{
"menu_caption": "Machine List",
"note1": "master",
"sub_menu_id": "1",
"route_path": "/machine_list"
},
{
"menu_caption": "Sparepart Category",
"note1": "master",
"sub_menu_id": "1",
"route_path": "/sparepart_category"
}
]
},
{
"appl_id": "MTC",
"menu_caption": "Master",
"menu_path": "/master",
"submenus": [
{
"menu_caption": "Machine List",
"note1": "master",
"sub_menu_id": "1",
"route_path": "/machine_list"
}
]
},
]
}
}
and I already create a model class using json to dart (and already retouch it a bit) and use json annotation and build runner. Then I get an error everytime I tried to decode the body of this json
if (jsonResponse.statusCode == 200) {
final jsonItems =
json.decode(jsonResponse.body).cast<Map<String, dynamic>>();
List<Profile> profile = jsonItems.map<Profile>((json) {
return MyMenu.fromJson(json);
}).toList();
return profile;
}
I always get this error message (attached on the image bellow):
What when wrong with my code ?
End up answering my question, I did this:
json.decode(jsonResponse.body)['data']['menus'].cast<Map<String, dynamic>>();
Don't know if there is any other perfect method to do this.
Your json had some syntax error as well.Please try this it if helps
import 'dart:convert';
void main() {
var jsonData= {
"status_code": 200,
"message": "Successfully get data user",
"data": {
"sec_user": {
"clientIp": "1.4.2.54",
"officeid": "N/A"
},
"username": "uname",
"name1": "",
"menus": [
{
"appl_id": "MTC",
"menu_caption": "Master",
"menu_path": "/master",
"submenus": [
{
"menu_caption": "Machine List",
"note1": "master",
"sub_menu_id": "1",
"route_path": "/machine_list"
},
{
"menu_caption": "Sparepart Category",
"note1": "master",
"sub_menu_id": "1",
"route_path": "/sparepart_category"
}
]
},
{
"appl_id": "MTC",
"menu_caption": "Master",
"menu_path": "/master",
"submenus": [
{
"menu_caption": "Machine List",
"note1": "master",
"sub_menu_id": "1",
"route_path": "/machine_list"
}
]
}
]
}
};
final jsonItems =
ApiResponse.fromJson(jsonData);
print(jsonItems.message);
}
class ApiResponse {
int statusCode;
String message;
Data data;
ApiResponse({this.statusCode, this.message, this.data});
ApiResponse.fromJson(Map<String, dynamic> json) {
statusCode = json['status_code'];
message = json['message'];
data = json['data'] != null ? new Data.fromJson(json['data']) : null;
}
Map<String, dynamic> toJson() {
final Map<String, dynamic> data = new Map<String, dynamic>();
data['status_code'] = this.statusCode;
data['message'] = this.message;
if (this.data != null) {
data['data'] = this.data.toJson();
}
return data;
}
}
class Data {
SecUser secUser;
String username;
String name1;
List<Menus> menus;
Data({this.secUser, this.username, this.name1, this.menus});
Data.fromJson(Map<String, dynamic> json) {
secUser = json['sec_user'] != null
? new SecUser.fromJson(json['sec_user'])
: null;
username = json['username'];
name1 = json['name1'];
if (json['menus'] != null) {
menus = new List<Menus>();
json['menus'].forEach((v) {
menus.add(new Menus.fromJson(v));
});
}
}
Map<String, dynamic> toJson() {
final Map<String, dynamic> data = new Map<String, dynamic>();
if (this.secUser != null) {
data['sec_user'] = this.secUser.toJson();
}
data['username'] = this.username;
data['name1'] = this.name1;
if (this.menus != null) {
data['menus'] = this.menus.map((v) => v.toJson()).toList();
}
return data;
}
}
class SecUser {
String clientIp;
String officeid;
SecUser({this.clientIp, this.officeid});
SecUser.fromJson(Map<String, dynamic> json) {
clientIp = json['clientIp'];
officeid = json['officeid'];
}
Map<String, dynamic> toJson() {
final Map<String, dynamic> data = new Map<String, dynamic>();
data['clientIp'] = this.clientIp;
data['officeid'] = this.officeid;
return data;
}
}
class Menus {
String applId;
String menuCaption;
String menuPath;
List<Submenus> submenus;
Menus({this.applId, this.menuCaption, this.menuPath, this.submenus});
Menus.fromJson(Map<String, dynamic> json) {
applId = json['appl_id'];
menuCaption = json['menu_caption'];
menuPath = json['menu_path'];
if (json['submenus'] != null) {
submenus = new List<Submenus>();
json['submenus'].forEach((v) {
submenus.add(new Submenus.fromJson(v));
});
}
}
Map<String, dynamic> toJson() {
final Map<String, dynamic> data = new Map<String, dynamic>();
data['appl_id'] = this.applId;
data['menu_caption'] = this.menuCaption;
data['menu_path'] = this.menuPath;
if (this.submenus != null) {
data['submenus'] = this.submenus.map((v) => v.toJson()).toList();
}
return data;
}
}
class Submenus {
String menuCaption;
String note1;
String subMenuId;
String routePath;
Submenus({this.menuCaption, this.note1, this.subMenuId, this.routePath});
Submenus.fromJson(Map<String, dynamic> json) {
menuCaption = json['menu_caption'];
note1 = json['note1'];
subMenuId = json['sub_menu_id'];
routePath = json['route_path'];
}
Map<String, dynamic> toJson() {
final Map<String, dynamic> data = new Map<String, dynamic>();
data['menu_caption'] = this.menuCaption;
data['note1'] = this.note1;
data['sub_menu_id'] = this.subMenuId;
data['route_path'] = this.routePath;
return data;
}
}

I need to merge nested json to simple json in dart flutter

I need to merge nested json to simple json here is the response from API
{
"1": [
{
"id": 6,
"name": "test 1"
},
{
"id": 8,
"name": "test 2"
},
{
"id": 7,
"name": "test 3"
}
],
"2": [
{
"id": 9,
"name": "ttt1"
},
{
"id": 5,
"name": "ttt3"
}
],
"3": [
{
"id": 4,
"name": "ttg"
}
]
}
How i need is
[
{
"id": 4,
"name": null
},
{
"id": 6,
"name": null
},
{
"id": 8,
"name": null
},
{
"id": 9,
"name": null
},
{
"id": 5,
"name": null
},
{
"id": 7,
"name": null
}
]
Here is my model
class HomeBannerModel {
int id;
String name;
HomeBannerModel({this.id, this.name});
HomeBannerModel.fromJson(Map<String, dynamic> json) {
id = json['id'];
image = json['name'];
}
Map<String, dynamic> toJson() {
final Map<String, dynamic> data = new Map<String, dynamic>();
data['id'] = this.id;
data['name'] = this.name;
return data;
}
}
SO here is my looping part
var responseList = response.data['data'];
if (response.data['success'] == true) {
List data = [];
responseList.forEach((k, v) {
var value =
v.map((banner) => HomeBannerModel.fromJson(banner)).toList();
data.add(value);
});
print(data);
but i am getting error as 'length' was called on null. so let me know where i am missing since i amnew to dart the api is giving numeric as key so i am facing the difficulty in getting the data
Maybe you can try this
List<HomeBannerModel> data = [];
responseList.forEach((key, value) {
value.forEach((element) {
data.add(HomeBannerModel.fromJson(element));
});
});

Dart - type 'String' is not a subtype of type 'Iterable<dynamic>'

I am trying to parse a local JSON file in dart and to display its values.
user.json
[
{
"id": 1,
"name": "Greg",
"imageUrl": "assets/images/greg.jpg"
},
{
"id": 2,
"name": "James",
"imageUrl": "assets/images/james.jpg"
}
]
Here is my chats.json
[
{
"sender": "james",
"time": "5:30 PM",
"text": "Hey, how's it going? What did you do today?",
"isLiked": false,
"unread": true
},
{
"sender": "olivia",
"time": "4:30 PM",
"text": "Hey, how's it going? What did you do today?",
"isLiked": true,
"unread": false
}
]
Here is my Message class
class Message {
final User sender;
final String time;
final String text;
final bool isLiked;
final bool unread;
Message({
this.sender,
this.time,
this.text,
this.isLiked,
this.unread,
});
factory Message.fromJson(Map<String, dynamic> parsedJson){
return Message(
sender: User.fromJson(parsedJson['sender']),
time : parsedJson['time'],
text : parsedJson ['text'],
isLiked : parsedJson['isLiked'],
unread : parsedJson ['unread'],
);
}
static List<User> parseUsers(usersJson) {
List<User> usersList = new List<User>.from(usersJson);
return usersList;
}
}
Here is my user class
class User {
final int id;
final String name;
final String imageUrl;
User({
this.id,
this.name,
this.imageUrl,
});
factory User.fromJson(Map<String, dynamic> parsedJson){
return User(
id: parsedJson['id'],
name : parsedJson['name'],
imageUrl : parsedJson ['imageUrl']
);
}
}
Here is my latest_chat.dart (it's StatefulWidget)
List<Message> _messages = List<Message>();
Future<String> _loadChatsAsset() async {
return await rootBundle.loadString('assets/json/chats.json');
}
Future<List<Message>> loadChats() async {
String jsonString = await _loadChatsAsset();
var messages = List<Message>();
final messagesJson = json.decode(jsonString);
for (var messageJson in messagesJson){
messages.add(Message.fromJson(messageJson));
}
return messages;
}
#override
void initState() {
loadChats().then((value) {
setState(() {
_messages.addAll(value);
});
});
super.initState();
}
To print something from my JSON I usually do _messages[index].sender.name or _messages[index].text etc...
The above parsing method worked without nested objects (for User only).
With nested objects (User inside Message) I am not able to identify the source of the following error.
type 'String' is not a subtype of type 'Iterable'
It's happening on this line of code
List usersList = new List.from(usersJson);
I am not sure what I am missing. Can you please assist?
Thanks in advance
As can be see in your json you are getting just string as a sender but when you are creating Message object then User class requires three arguments.
Means that, as a sender in json there should a user object.
{
"id" : 1
"name" : "Viren"
"imageUrl" : 'someurl'
}
Your json and your desire output is not matching.
Moreover your following line is also wrong.
sender: parseUsers(parsedJson['sender']),
Change to this.
sender: User.fromJson(parsedJson['sender'])
Update:
Your json should be like below.
[
{
"sender": {
"id": 1,
"name": "Greg",
"imageUrl": "assets/images/greg.jpg"
},
"time": "5:30 PM",
"text": "Hey, how's it going? What did you do today?",
"isLiked": false,
"unread": true
},
{
"sender": {
"id": 2,
"name": "James",
"imageUrl": "assets/images/james.jpg"
},
"time": "4:30 PM",
"text": "Hey, how's it going? What did you do today?",
"isLiked": true,
"unread": false
}
]

How to get json array in Flutter/Dart

I would want to get the data for all "name" only from the array data.
I want to print(data['data']['name']);
But it returns this error:
Unhandled Exception: type 'String' is not a subtype of type 'int' of 'index'
But when I print(data['data']);, it will return all data from "data":
"data": [
{
"created_at": "2020-03-16 16:10:51",
"deleted_at": null,
"id": 2,
"is_active": 1,
"name": "Maybank",
"updated_at": "2020-03-16 16:18:06"
},
{
"created_at": "2020-03-16 16:27:37",
......
],
Call API Code
displayBanks(BuildContext context) async {
_callApi.refreshTokenApi(context);
var _addressUrl = '$_hostUrl/banks'; //API URL
final SharedPreferences prefs = await SharedPreferences.getInstance();
_accessToken = prefs.getString('access_token');
Response _response = await get(_addressUrl, headers: {
'Content-type': 'application/json',
'Accept': 'application/json',
'Authorization': 'Bearer $_accessToken'
});
var data;
data = jsonDecode(_response.body);
if (_response.statusCode == 200) {
print(data['data']['name']);
return data;
}
else {
print(_response.statusCode);
}
}
SAMPLE JSON DATA FROM API URL:
{
"data": [
{
"created_at": "2020-03-16 16:10:51",
"deleted_at": null,
"id": 2,
"is_active": 1,
"name": "Maybank",
"updated_at": "2020-03-16 16:18:06"
},
{
"created_at": "2020-03-16 16:27:37",
"deleted_at": null,
"id": 3,
"is_active": 1,
"name": "India International Bank (Malaysia) Berhad",
"updated_at": "2020-03-16 16:27:37"
},
{
"created_at": "2020-03-16 16:27:37",
"deleted_at": null,
"id": 4,
"is_active": 1,
"name": "National Bank of Abu Dhabi Malaysia Berhad",
"updated_at": "2020-03-16 16:27:37"
}
],
"links": {
"first": "https://demo.local/api/banks?page=1",
"last": "https://demo.local/api/banks?page=1",
"next": null,
"prev": null
},
"meta": {
"current_page": 1,
"from": 1,
"last_page": 1,
"path": "https://demo.local/api/banks",
"per_page": 5,
"to": 3,
"total": 3
}
}
Unhandled Exception: type 'String' is not a subtype of type 'int' of 'index'
The exception message explains the issue clearly.
The property 'name' is inside an object which itself placed in an array. So you first decode the array. Then access each object using the index (0..n), then from each object, you can read the 'name' property.
Here you go
class MyData {
final List<Data> data;
MyData({this.data});
factory MyData.fromJson(Map<String, dynamic> json) {
return MyData(
data: json['data'] != null ? (json['data'] as List).map((i) => Data.fromJson(i)).toList() : null,
);
}
Map<String, dynamic> toJson() {
final Map<String, dynamic> data = new Map<String, dynamic>();
if (this.data != null) {
data['data'] = this.data.map((v) => v.toJson()).toList();
}
return data;
}
}
class Data {
final String created_at;
final String deleted_at;
final int id;
final int is_active;
final String name;
final String updated_at;
Data({this.created_at, this.deleted_at, this.id, this.is_active, this.name, this.updated_at});
factory Data.fromJson(Map<String, dynamic> json) {
return Data(
created_at: json['created_at'],
deleted_at: json['deleted_at'],
id: json['id'],
is_active: json['is_active'],
name: json['name'],
updated_at: json['updated_at'],
);
}
Map<String, dynamic> toJson() {
final Map<String, dynamic> data = new Map<String, dynamic>();
data['created_at'] = this.created_at;
data['id'] = this.id;
data['is_active'] = this.is_active;
data['name'] = this.name;
data['updated_at'] = this.updated_at;
data['deleted_at'] = this.deleted_at;
return data;
}
}
The error makes sense. The 'data' attribute in your JSON is array. So, you'll have to pass index of the item to access 'name' attribute - something like - data['data'][0]['name'] to get 'Maybank'.
Ideally, you should have a class which creates the instance from the JSON. In this case, the code snippet will look like :
Banks banks = new Banks.fromJson(data)
Now, you can use sites like this to create a class definition (including.fromJson).