0

in my application i get this result from server which into categories, that have an array which named products :

{
  "categories": [
    {
      "id": 1,
      "store_id": 1,
      "category_id": null,
      "page_id": null,
      "title": "cat_name",
      "image_uri": "/uploads/store/category_images/2020/1600839088.jpg",
      "created_at": "2020-09-23T02:01:28.000000Z",
      "updated_at": "2020-09-23T02:01:57.000000Z",
      "products": [
        {
          "id": 1,
          "store_categories_id": 1,
          "store_id": 1,
          "title": "title",
          "description": "111111",
          //...
        }
      ]
    }
  ],
  "slides": {
    "id": 1,
    "slide_1": "/uploads/store/store_sliders/2020/16025126045410.jpg",
    //...
  }
}

i created this class with above structure:

StoreCategories class:

@JsonSerializable()
class StoreCategories {
  final List<StoreCategoriesList> categories;

  @JsonKey(nullable: true)
  final Slides slides;

  StoreCategories(this.categories,this.slides);

  factory StoreCategories.fromJson(Map<String, dynamic> json) => _$StoreCategoriesFromJson(json);

  Map<String, dynamic> toJson() => _$StoreCategoriesToJson(this);
}

StoreCategoriesList class:

@JsonSerializable()
class StoreCategoriesList{
  final int id;

  @JsonKey(name: 'store_id')
  final int storeId;

  final String title;

  @JsonKey(name: 'image_uri')
  final String imageUri;

  @JsonKey(nullable: true)
  final List<ProductsList> products;

  StoreCategoriesList(this.id, this.storeId, this.title, this.imageUri, this.products);

  factory StoreCategoriesList.fromJson(Map<String, dynamic> json) => _$StoreCategoriesListFromJson(json);

  Map<String, dynamic> toJson() => _$StoreCategoriesListToJson(this);
}

ProductsList class:

@JsonSerializable()
class ProductsList {
  final int id;

  @JsonKey(name: 'store_categories_id')
  final int storeCategoriesId;

  @JsonKey(name: 'store_id')
  final int storeId;

  final String title;
  final String description;
  final String image;
  final int cost;

  ProductsList(this.id, this.storeCategoriesId, this.storeId, this.title, this.description, this.image, this.cost);

  factory ProductsList.fromJson(Map<String, dynamic> json) => _$ProductsListFromJson(json);

  Map<String, dynamic> toJson() => _$ProductsListToJson(this);
}

now! how can i convert my json structure to class map?

this code is not correct:

StoreCategories.fromJson(_res.response.data.map((data) => StoreCategories.fromJson(data)));

//StoreCategories.fromJson(_res.response.data.map((data) => List<StoreCategories>.fromJson(data)));

//StoreCategories.fromJson(_res.response.data.map((data) => List<StoreCategories>.from(data)));

1 Answer 1

1

Sounds like your _res.response.data is already a Map or List right?

So just simply StoreCategories.fromJson(_res.response.data). Done!

Flutter json_serializable is smart enough to decode nested classes! :)

P.S. If you have a JSON String, do jsonDecode(your_json_string) to get a Map or List.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.