I am trying to fetch data from api with Dio , But I found an error in my debug console - Instance of 'CategoryModel' . I don't know why I face this error.
My Problem Issue ScreenShot
The Json File
{
"categories": [{
"id": 1,
"name": "Fruits & Vegetables",
"slug": "fruits--vegetables",
"image": "public\/uploads\/category\/1642326298-fruits-and-vegetables.png",
"frontProduct": "1",
"level": null,
"status": "1",
"created_at": "2022-01-01T18:15:36.000000Z",
"updated_at": "2022-01-15T22:44:58.000000Z",
"subcategories": [{
"id": 1,
"subcategoryName": "Formula and Energy Boosters",
"slug": "formula-and-energy-boosters",
"image": "public\/uploads\/subcategory\/1642051209-formula-n-energy-boosters.jpg",
"banner": "public\/uploads\/subcategory\/1642060781-banner.jpg",
"category_id": "1",
"status": "1",
"created_at": "2022-01-01T19:00:19.000000Z",
"updated_at": "2022-01-12T20:59:41.000000Z",
"childcategories": [{
"id": 1,
"childcategoryName": "Stylish T-shirts",
"slug": "stylish-t-shirts",
"image": null,
"banner": null,
"subcategory_id": "1",
"status": "1",
"created_at": "2022-01-01T19:02:45.000000Z",
"updated_at": "2022-01-01T19:02:45.000000Z"
}]
}, {
"id": 2,
"subcategoryName": "Baby Milk and Foods",
"slug": "baby-milk-and-foods",
"image": "public\/uploads\/subcategory\/1642053444-baby-milk-and-food.jpg",
"banner": null,
"category_id": "1",
"status": "1",
"created_at": "2022-01-01T19:07:13.000000Z",
"updated_at": "2022-01-12T18:57:24.000000Z",
"childcategories": [{
"id": 2,
"childcategoryName": "Shirt Collection",
"slug": "shirt-collection",
"image": null,
"banner": null,
"subcategory_id": "2",
"status": "1",
"created_at": "2022-01-01T19:08:35.000000Z",
"updated_at": "2022-01-01T19:08:35.000000Z"
}]
}, {
"id": 3,
"subcategoryName": "Diapers",
"slug": "diapers",
"image": "public\/uploads\/subcategory\/1642053483-diaper.jpg",
"banner": null,
"category_id": "1",
"status": "1",
"created_at": "2022-01-01T19:25:37.000000Z",
"updated_at": "2022-01-12T18:58:03.000000Z",
"childcategories": [{
"id": 5,
"childcategoryName": "Jeans Pant",
"slug": "jeans-pant",
"image": null,
"banner": null,
"subcategory_id": "3",
"status": "1",
"created_at": "2022-01-01T19:39:23.000000Z",
"updated_at": "2022-01-01T19:39:23.000000Z"
}]
}]
}, {
"id": 2,
"name": "Meat & Fish",
"slug": "meat--fish",
"image": "public\/uploads\/category\/1642326372-meat-and-fish.webp",
"frontProduct": "1",
"level": null,
"status": "1",
"created_at": "2022-01-01T18:17:51.000000Z",
"updated_at": "2022-01-15T22:46:12.000000Z",
"subcategories": [{
"id": 4,
"subcategoryName": "Saree",
"slug": "saree",
"image": "public\/uploads\/subcategory\/1641816956-subcategory-6.jpg",
"banner": null,
"category_id": "2",
"status": "1",
"created_at": "2022-01-01T19:27:23.000000Z",
"updated_at": "2022-01-10T01:15:56.000000Z",
"childcategories": [{
"id": 6,
"childcategoryName": "Cotton Saree",
"slug": "cotton-saree",
"image": null,
"banner": null,
"subcategory_id": "4",
"status": "1",
"created_at": "2022-01-01T19:40:00.000000Z",
"updated_at": "2022-01-01T19:40:00.000000Z"
}]
}, {
"id": 5,
"subcategoryName": "Salwar Kamiz",
"slug": "salwar-kamiz",
"image": "public\/uploads\/subcategory\/1641816945-subcategory-7.jpg",
"banner": null,
"category_id": "2",
"status": "1",
"created_at": "2022-01-01T19:28:02.000000Z",
"updated_at": "2022-01-10T01:15:45.000000Z",
"childcategories": [{
"id": 3,
"childcategoryName": "Original Indian Dress",
"slug": "original-indian-dress",
"image": null,
"banner": null,
"subcategory_id": "5",
"status": "1",
"created_at": "2022-01-01T19:29:25.000000Z",
"updated_at": "2022-01-01T19:29:25.000000Z"
}, {
"id": 4,
"childcategoryName": "Block Batik Dress",
"slug": "block-batik-dress",
"image": null,
"banner": null,
"subcategory_id": "5",
"status": "1",
"created_at": "2022-01-01T19:30:12.000000Z",
"updated_at": "2022-01-01T19:30:12.000000Z"
}]
}, {
"id": 6,
"subcategoryName": "Western",
"slug": "western",
"image": "public\/uploads\/subcategory\/1641816838-subcategory-3.jpg",
"banner": null,
"category_id": "2",
"status": "1",
"created_at": "2022-01-01T19:28:31.000000Z",
"updated_at": "2022-01-10T01:13:58.000000Z",
"childcategories": [{
"id": 7,
"childcategoryName": "Tops",
"slug": "tops",
"image": null,
"banner": null,
"subcategory_id": "6",
"status": "1",
"created_at": "2022-01-01T19:40:30.000000Z",
"updated_at": "2022-01-01T19:40:30.000000Z"
}, {
"id": 8,
"childcategoryName": "Leggings",
"slug": "leggings",
"image": null,
"banner": null,
"subcategory_id": "6",
"status": "1",
"created_at": "2022-01-01T19:40:46.000000Z",
"updated_at": "2022-01-01T19:40:46.000000Z"
}]
}]
}, {
"id": 3,
"name": "Milk & Dairy",
"slug": "milk--dairy",
"image": "public\/uploads\/category\/1642326427-milk-and-dairy.png",
"frontProduct": null,
"level": null,
"status": "1",
"created_at": "2022-01-15T22:47:07.000000Z",
"updated_at": "2022-01-15T22:47:07.000000Z",
"subcategories": [{
"id": 7,
"subcategoryName": "Dress",
"slug": "dress",
"image": "public\/uploads\/subcategory\/1641816822-subcategory-2.jpg",
"banner": null,
"category_id": "3",
"status": "1",
"created_at": "2022-01-01T19:55:10.000000Z",
"updated_at": "2022-01-10T01:13:42.000000Z",
"childcategories": [{
"id": 11,
"childcategoryName": "Shirt",
"slug": "shirt",
"image": null,
"banner": null,
"subcategory_id": "7",
"status": "1",
"created_at": "2022-01-01T19:58:39.000000Z",
"updated_at": "2022-01-01T19:58:39.000000Z"
}, {
"id": 12,
"childcategoryName": "Pant",
"slug": "pant",
"image": "public\/uploads\/childcategory\/1642335874-pant-logo.jpg",
"banner": "public\/uploads\/childcategory\/1642335874-pants-banner.png",
"subcategory_id": "7",
"status": "1",
"created_at": "2022-01-01T19:59:05.000000Z",
"updated_at": "2022-01-16T01:24:34.000000Z"
}]
}, {
"id": 8,
"subcategoryName": "Footwear",
"slug": "footwear",
"image": "public\/uploads\/subcategory\/1641816595-subcategory.jpg",
"banner": "public\/uploads\/subcategory\/1641821574-kids-banner.png",
"category_id": "3",
"status": "1",
"created_at": "2022-01-01T19:55:36.000000Z",
"updated_at": "2022-01-10T02:32:54.000000Z",
"childcategories": [{
"id": 9,
"childcategoryName": "Shoe",
"slug": "shoe",
"image": null,
"banner": null,
"subcategory_id": "8",
"status": "1",
"created_at": "2022-01-01T19:57:23.000000Z",
"updated_at": "2022-01-01T19:57:23.000000Z"
}, {
"id": 10,
"childcategoryName": "Leather Sandal",
"slug": "leather-sandal",
"image": null,
"banner": null,
"subcategory_id": "8",
"status": "1",
"created_at": "2022-01-01T19:58:02.000000Z",
"updated_at": "2022-01-01T19:58:02.000000Z"
}]
}]
}, {
"id": 4,
"name": "Food",
"slug": "food",
"image": "public\/uploads\/category\/1642326638-food.jpg",
"frontProduct": "1",
"level": null,
"status": "1",
"created_at": "2022-01-01T18:24:59.000000Z",
"updated_at": "2022-01-15T22:50:38.000000Z",
"subcategories": []
}, {
"id": 5,
"name": "Baby Care",
"slug": "baby-care",
"image": "public\/uploads\/category\/1642326689-baby-care.png",
"frontProduct": "1",
"level": null,
"status": "1",
"created_at": "2022-01-01T18:27:28.000000Z",
"updated_at": "2022-01-15T22:51:29.000000Z",
"subcategories": []
}, {
"id": 6,
"name": "Health & Beauty",
"slug": "health--beauty",
"image": "public\/uploads\/category\/1642326774-health-and-beauty.png",
"frontProduct": "1",
"level": null,
"status": "1",
"created_at": "2022-01-01T18:43:51.000000Z",
"updated_at": "2022-01-15T22:52:54.000000Z",
"subcategories": []
}, {
"id": 7,
"name": "Office & Stationery",
"slug": "office--stationery",
"image": "public\/uploads\/category\/1642326827-office-stationary.png",
"frontProduct": null,
"level": null,
"status": "1",
"created_at": "2022-01-15T22:53:47.000000Z",
"updated_at": "2022-01-15T22:53:47.000000Z",
"subcategories": []
}, {
"id": 8,
"name": "Pet Care",
"slug": "pet-care",
"image": "public\/uploads\/category\/1642326888-pet-care.png",
"frontProduct": "1",
"level": null,
"status": "1",
"created_at": "2022-01-15T22:54:48.000000Z",
"updated_at": "2022-01-15T22:54:48.000000Z",
"subcategories": [{
"id": 9,
"subcategoryName": "Kitten Food",
"slug": "kitten-food",
"image": "public\/uploads\/subcategory\/1642334752-kitten-food.jpg",
"banner": "public\/uploads\/subcategory\/1642334752-kitten-food-banner.jpg",
"category_id": "8",
"status": "1",
"created_at": "2022-01-16T01:05:52.000000Z",
"updated_at": "2022-01-16T01:05:52.000000Z",
"childcategories": []
}]
}, {
"id": 9,
"name": "Toys & Games",
"slug": "toys--games",
"image": "public\/uploads\/category\/1642327094-toy-and-games.jpg",
"frontProduct": "1",
"level": null,
"status": "1",
"created_at": "2022-01-15T22:58:14.000000Z",
"updated_at": "2022-01-15T22:58:14.000000Z",
"subcategories": []
}, {
"id": 10,
"name": "Home & Cleaning",
"slug": "home--cleaning",
"image": "public\/uploads\/category\/1642327182-house-cleaning.jpg",
"frontProduct": null,
"level": null,
"status": "1",
"created_at": "2022-01-15T22:59:42.000000Z",
"updated_at": "2022-01-15T22:59:42.000000Z",
"subcategories": []
}, {
"id": 11,
"name": "Sports & Fitness",
"slug": "sports--fitness",
"image": "public\/uploads\/category\/1642327903-sports-and-fitness.jpg",
"frontProduct": null,
"level": null,
"status": "1",
"created_at": "2022-01-15T23:11:43.000000Z",
"updated_at": "2022-01-15T23:11:43.000000Z",
"subcategories": []
}, {
"id": 12,
"name": "Kitchen & Home Appliances",
"slug": "kitchen--home-appliances",
"image": "public\/uploads\/category\/1642327984-home-and-kitchen.png",
"frontProduct": null,
"level": null,
"status": "1",
"created_at": "2022-01-15T23:13:04.000000Z",
"updated_at": "2022-01-15T23:13:04.000000Z",
"subcategories": []
}]
}
Here Is My all Code -
CategoryModel.dart
file
import 'package:Darucheeni/src/models/subCategoryModel.dart';
import 'package:json_annotation/json_annotation.dart';
part 'categoryModel.g.dart';
@JsonSerializable()
class CategoryModel {
final int? id;
final String? name;
final String? slug;
final String? image;
final SubCategoryModel? subCategoryModel;
CategoryModel({
required this.id,
required this.name,
required this.slug,
required this.image,
required this.subCategoryModel,
});
factory CategoryModel.fromJson(Map<String, dynamic> json) =>
_$CategoryModelFromJson(json);
Map<String, dynamic> toJson() => _$CategoryModelToJson(this);
}
Here Is CategoryModel.g.dart
// GENERATED CODE - DO NOT MODIFY BY HAND
part of 'categoryModel.dart';
// **************************************************************************
// JsonSerializableGenerator
// **************************************************************************
CategoryModel _$CategoryModelFromJson(Map<String, dynamic> json) =>
CategoryModel(
id: json['id'] as int?,
name: json['name'] as String?,
slug: json['slug'] as String?,
image: json['image'] as String?,
subCategoryModel: json['subCategoryModel'] == null
? null
: SubCategoryModel.fromJson(
json['subCategoryModel'] as Map<String, dynamic>),
);
Map<String, dynamic> _$CategoryModelToJson(CategoryModel instance) =>
<String, dynamic>{
'id': instance.id,
'name': instance.name,
'slug': instance.slug,
'image': instance.image,
'subCategoryModel': instance.subCategoryModel,
};
Here Is my categoryController.dart
file
import 'package:Darucheeni/src/configs/appConfigs.dart';
import 'package:Darucheeni/src/models/categoryModel.dart';
import 'package:dio/dio.dart';
import 'package:get/get.dart';
class CategoryController extends GetxController {
final dio = Dio();
final allCategoryList = RxList<CategoryModel>();
getAllCategory() async {
try {
final res = await dio.get(baseUrl 'all-category');
final List<CategoryModel> categoryData = res.data['categories']
.map((json) => CategoryModel.fromJson(json))
.toList()
.cast<CategoryModel>();
print('------------------------------>');
print('Status Code : ${res.statusCode}');
print('Headers : ${res.headers}');
print('realUri : ${res.realUri}');
print('statusMessage : ${res.statusMessage}');
print(res.data);
print('Category Data : $categoryData');
print('requestOptions : ${res.requestOptions}');
// print(res.body);
// print(res.unauthorized);
print('------------------------------>');
if (res.data == 200) {
allCategoryList.addAll(
(res.data as List).map((e) => CategoryModel.fromJson(e)).toList());
}
} catch (e) {
print(e);
}
}
}
CodePudding user response:
If you want to print a class, you should override 'toString' method. Console is don't know how to print classes
import 'package:Darucheeni/src/models/subCategoryModel.dart';
import 'package:json_annotation/json_annotation.dart';
part 'categoryModel.g.dart';
@JsonSerializable()
class CategoryModel {
final int? id;
final String? name;
final String? slug;
final String? image;
final SubCategoryModel? subCategoryModel;
CategoryModel({
required this.id,
required this.name,
required this.slug,
required this.image,
required this.subCategoryModel,
});
factory CategoryModel.fromJson(Map<String, dynamic> json) =>
_$CategoryModelFromJson(json);
Map<String, dynamic> toJson() => _$CategoryModelToJson(this);
@override
String toString() {
return 'CategoryModel{ id: $id, name: $name, slug: $slug, image: $image, SubCategoryModel: $subCategoryModel}';
}
}