I am storing json object in a model and trying to access properties of my model but I am not able to access my properties which is in my model following is my json object which I am storing in model
{
"statusCode": 200,
"success": true,
"messages": [],
"data": [
{
"id": 35,
"title": "Astrology",
"filename": "Astrology.jpg",
"mimetype": "image/jpeg",
"directcalling": 1,
"parentid": null,
"subcat": [
{
"id": 53,
"title": "vgb",
"filename": "vgb.jpg",
"mimetype": "image/jpeg",
"directcalling": 0,
"parentid": 35,
"subcat": []
}
]
},
{
"id": 36,
"title": "Muhurtam",
"filename": "Muhurtam.jpg",
"mimetype": "image/jpeg",
"directcalling": 1,
"parentid": null,
"subcat": [
{
"id": 50,
"title": "abc",
"filename": "abc.png",
"mimetype": "image/png",
"directcalling": 0,
"parentid": 36,
"subcat": []
}
]
},
{
"id": 37,
"title": "DharamSandhehalu",
"filename": "DharamSandhehalu.jpg",
"mimetype": "image/jpeg",
"directcalling": 1,
"parentid": null,
"subcat": []
},
{
"id": 38,
"title": "Allpoojas",
"filename": "Allpoojas.jpg",
"mimetype": "image/jpeg",
"directcalling": 0,
"parentid": null,
"subcat": [
{
"id": 40,
"title": "gugu",
"filename": "gugu.png",
"mimetype": "image/png",
"directcalling": 0,
"parentid": 38,
"subcat": []
}
]
}
]
}
following is my model class where I am storing my json object
class Categories {
Categories({
required this.statusCode,
required this.success,
required this.messages,
required this.data,
});
late final int statusCode;
late final bool success;
late final List<dynamic> messages;
late final List<Data> data;
Categories.fromJson(Map<String, dynamic> json) {
statusCode = json['statusCode'];
success = json['success'];
messages = List.castFrom<dynamic, dynamic>(json['messages']);
data = List.from(json['data']).map((e) => Data.fromJson(e)).toList();
}
Map<String, dynamic> toJson() {
final _data = <String, dynamic>{};
_data['statusCode'] = statusCode;
_data['success'] = success;
_data['messages'] = messages;
_data['data'] = data.map((e) => e.toJson()).toList();
return _data;
}
}
class Data {
Data({
required this.id,
required this.title,
required this.filename,
required this.mimetype,
required this.directcalling,
this.parentid,
required this.subcat,
});
late final int id;
late final String title;
late final String filename;
late final String mimetype;
late final int directcalling;
late final Null parentid;
late final List<Subcat> subcat;
Data.fromJson(Map<String, dynamic> json) {
id = json['id'];
title = json['title'];
filename = json['filename'];
mimetype = json['mimetype'];
directcalling = json['directcalling'];
parentid = null;
subcat = List.from(json['subcat']).map((e) => Subcat.fromJson(e)).toList();
}
Map<String, dynamic> toJson() {
final _data = <String, dynamic>{};
_data['id'] = id;
_data['title'] = title;
_data['filename'] = filename;
_data['mimetype'] = mimetype;
_data['directcalling'] = directcalling;
_data['parentid'] = parentid;
_data['subcat'] = subcat.map((e) => e.toJson()).toList();
return _data;
}
}
class Subcat {
Subcat({
required this.id,
required this.title,
required this.filename,
required this.mimetype,
required this.directcalling,
required this.parentid,
required this.subcat,
});
late final int id;
late final String title;
late final String filename;
late final String mimetype;
late final int directcalling;
late final int parentid;
late final List<dynamic> subcat;
Subcat.fromJson(Map<String, dynamic> json) {
id = json['id'];
title = json['title'];
filename = json['filename'];
mimetype = json['mimetype'];
directcalling = json['directcalling'];
parentid = json['parentid'];
subcat = List.castFrom<dynamic, dynamic>(json['subcat']);
}
Map<String, dynamic> toJson() {
final _data = <String, dynamic>{};
_data['id'] = id;
_data['title'] = title;
_data['filename'] = filename;
_data['mimetype'] = mimetype;
_data['directcalling'] = directcalling;
_data['parentid'] = parentid;
_data['subcat'] = subcat;
return _data;
}
}
following is my api where I am getting response and storing in my model
Future<void> getCatogories(BuildContext cont) async {
final url = PurohitApi().baseUrl PurohitApi().getcatogory;
try {
var response = await http.get(
Uri.parse(url),
headers: {
'Content-Type': 'application/json; charset=UTF-8',
},
);
Map<String, dynamic> categoryTypes = json.decode(response.body);
if (categoryTypes['data'] != null) {
categories = categoryTypes['data'];
}
categorieModel = Categories.fromJson(categoryTypes);
print(sample!.filename);
notifyListeners();
} catch (e) {
print(e);
}
}
now I am trying to access list which is in categories class which I am not getting how to do
CodePudding user response:
Try following demo
import 'package: flutter/material.dart';
import 'dart:convert';
const Color darkBlue = Color.fromARGB(255, 18, 32, 47);
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
theme: ThemeData.dark().copyWith(
scaffoldBackgroundColor: darkBlue,
),
debugShowCheckedModeBanner: false,
home: Scaffold(
body: Center(
child: MyWidget(),
),
),
);
}
}
class MyWidget extends StatelessWidget {
String jsonString = "{"
" \"statusCode\": 200,"
" \"success\": true,"
" \"messages\": [],"
" \"data\": ["
" {"
" \"id\": 35,"
" \"title\": \"Astrology\","
" \"filename\": \"Astrology.jpg\","
" \"mimetype\": \"image/jpeg\","
" \"directcalling\": 1,"
" \"parentid\": null,"
" \"subcat\": ["
" {"
" \"id\": 53,"
" \"title\": \"vgb\","
" \"filename\": \"vgb.jpg\","
" \"mimetype\": \"image/jpeg\","
" \"directcalling\": 0,"
" \"parentid\": 35,"
" \"subcat\": []"
" }"
" ]"
" },"
" {"
" \"id\": 36,"
" \"title\": \"Muhurtam\","
" \"filename\": \"Muhurtam.jpg\","
" \"mimetype\": \"image/jpeg\","
" \"directcalling\": 1,"
" \"parentid\": null,"
" \"subcat\": ["
" {"
" \"id\": 50,"
" \"title\": \"abc\","
" \"filename\": \"abc.png\","
" \"mimetype\": \"image/png\","
" \"directcalling\": 0,"
" \"parentid\": 36,"
" \"subcat\": []"
" }"
" ]"
" },"
" {"
" \"id\": 37,"
" \"title\": \"DharamSandhehalu\","
" \"filename\": \"DharamSandhehalu.jpg\","
" \"mimetype\": \"image/jpeg\","
" \"directcalling\": 1,"
" \"parentid\": null,"
" \"subcat\": []"
" },"
" {"
" \"id\": 38,"
" \"title\": \"Allpoojas\","
" \"filename\": \"Allpoojas.jpg\","
" \"mimetype\": \"image/jpeg\","
" \"directcalling\": 0,"
" \"parentid\": null,"
" \"subcat\": ["
" {"
" \"id\": 40,"
" \"title\": \"gugu\","
" \"filename\": \"gugu.png\","
" \"mimetype\": \"image/png\","
" \"directcalling\": 0,"
" \"parentid\": 38,"
" \"subcat\": []"
" }"
" "
" ]"
" }"
" ]"
"}";
@override
Widget build(BuildContext context) {
return
ListView.builder(
itemCount: Categories.fromJson(json.decode(jsonString)).data.length,
itemBuilder: (BuildContext context, int index) {
return ListTile(
leading: const Icon(Icons.list),
trailing: Text(
(Categories.fromJson(json.decode(jsonString)).data[index].subcat.length.toString()),
style: TextStyle(color: Colors.green, fontSize: 15),
),
title: Text((Categories.fromJson(json.decode(jsonString)).data[index].title)));
});
}
}
class Categories {
Categories({
required this.statusCode,
required this.success,
required this.messages,
required this.data,
});
late final int statusCode;
late final bool success;
late final List<dynamic> messages;
late final List<Data> data;
Categories.fromJson(Map<String, dynamic> json) {
statusCode = json['statusCode'];
success = json['success'];
messages = List.castFrom<dynamic, dynamic>(json['messages']);
data = List.from(json['data']).map((e) => Data.fromJson(e)).toList();
}
Map<String, dynamic> toJson() {
final _data = <String, dynamic>{};
_data['statusCode'] = statusCode;
_data['success'] = success;
_data['messages'] = messages;
_data['data'] = data.map((e) => e.toJson()).toList();
return _data;
}
}
class Data {
Data({
required this.id,
required this.title,
required this.filename,
required this.mimetype,
required this.directcalling,
this.parentid,
required this.subcat,
});
late final int id;
late final String title;
late final String filename;
late final String mimetype;
late final int directcalling;
late final Null parentid;
late final List<Subcat> subcat;
Data.fromJson(Map<String, dynamic> json) {
id = json['id'];
title = json['title'];
filename = json['filename'];
mimetype = json['mimetype'];
directcalling = json['directcalling'];
parentid = null;
subcat = List.from(json['subcat']).map((e) => Subcat.fromJson(e)).toList();
}
Map<String, dynamic> toJson() {
final _data = <String, dynamic>{};
_data['id'] = id;
_data['title'] = title;
_data['filename'] = filename;
_data['mimetype'] = mimetype;
_data['directcalling'] = directcalling;
_data['parentid'] = parentid;
_data['subcat'] = subcat.map((e) => e.toJson()).toList();
return _data;
}
}
class Subcat {
Subcat({
required this.id,
required this.title,
required this.filename,
required this.mimetype,
required this.directcalling,
required this.parentid,
required this.subcat,
});
late final int id;
late final String title;
late final String filename;
late final String mimetype;
late final int directcalling;
late final int parentid;
late final List<dynamic> subcat;
Subcat.fromJson(Map<String, dynamic> json) {
id = json['id'];
title = json['title'];
filename = json['filename'];
mimetype = json['mimetype'];
directcalling = json['directcalling'];
parentid = json['parentid'];
subcat = List.castFrom<dynamic, dynamic>(json['subcat']);
}
Map<String, dynamic> toJson() {
final _data = <String, dynamic>{};
_data['id'] = id;
_data['title'] = title;
_data['filename'] = filename;
_data['mimetype'] = mimetype;
_data['directcalling'] = directcalling;
_data['parentid'] = parentid;
_data['subcat'] = subcat;
return _data;
}
}