DolDurma
DolDurma

Reputation: 17321

Flutter convert json with array child and object to class map

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

Upvotes: 0

Views: 662

Answers (1)

ch271828n
ch271828n

Reputation: 17597

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.

Upvotes: 1

Related Questions