简体   繁体   English

我该如何解决这个问题——“CategoryModel”的实例? Flutter

[英]How can I solve this problem - Instance of 'CategoryModel'? Flutter

I am trying to fetch data from api with Dio, But I found an error in my debug console - Instance of 'CategoryModel' .我正在尝试使用 Dio 从 api 获取数据,但我在调试控制台中发现了一个错误 - Instance of 'CategoryModel' I don't know why I face this error.我不知道为什么我会遇到这个错误。

My Problem Issue ScreenShot我的问题问题截图

这是我的问题问题截图

The Json File Json 文件

{
    "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 CategoryModel.dart文件

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这是 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这是我的categoryController.dart文件

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);
    }
  }
}

If you want to print a class, you should override 'toString' method.如果你想打印 class,你应该覆盖 'toString' 方法。 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}';
  }

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我该如何解决这个数据库问题?[json] - How can I solve this database problem?[json] 我该如何解决这个空的 dataframe 问题? - How can i solve this empty dataframe problem? 我如何在React Native中解决uniqe关键道具问题 - How can i solve uniqe key prop problem in React native 如何解决 Ansible 的 url 分页循环问题? - How can I solve the url pagination loop problem with Ansible? 如何在一个扑动的应用程序中解决json对象重复的问题? - How to solve the problem of json objects duplication in a flutter app? 我如何解决这个错误=“NoSuchMethodrror:Class'String'没有实例getter'statusCode'。” - How to Can I solve this error= "NoSuchMethodrror: Class 'String' has no instance getter 'statusCode'." 使用 json.loads 返回数据时出现问题,我无法解决 - Problem returning data with json.loads, I can't solve it 将 json 发布到我的 web-api 的问题。 我该如何解决 - Problem with posting json to my web-api. How can i solve it 我该如何解决这个 kibana 控制台错误 - How can i solve this kibana console error 如何添加utf8解码器解决flutter http请求中的乱码问题? - How to add utf8 decoder to solve the problem of Garbled characters in flutter http request?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM