Home > Net >  Print nested object in Dart
Print nested object in Dart

Time:04-22

new to Dart and new to programming in general. I'm try to understand how to print a nested json object inside list. I figure out how to print a single value or the entire json parsed string but don't understand how to print (one after one) every values of json nested object.

import 'dart:convert';



void main() {

  
  var jsonString = '{"id":"3fa85f64-5717-4562-b3fc-2c963f66afa6","name":"string","restaurantId":"3fa85f64-5717-4562-b3fc-2c963f66afa6","items":[{"id":"3fa85f64-5717-4562-b3fc-2c963f6xxxxxxxx","name":"string","description":"string","hasImage":true,"active":true,"imgURL":"string","price":0,"menuId":"3fa85f64-5717-4562-b3fc-2c963f66afa6","childMenuItemId":"3fa85f64-5717-4562-b3fc-2c963f66afa6"}]}';
  var parsed = jsonDecode(jsonString);
  Map <String,dynamic> parsed2 = jsonDecode(jsonString);
  
  Menu menuItems = Menu.fromJson(parsed);
  Items itemsItems = Items.fromJson(parsed2);
  print(parsed);
  print(parsed2);
  print (menuItems.getIdMenu);
  print(itemsItems.idItems);
  
  
}




/* 
 * 
 * [
  {
    "id": "3fa85f64-5717-4562-b3fc-2c963f66afa6",
    "name": "string",
    "restaurantId": "3fa85f64-5717-4562-b3fc-2c963f66afa6",
    "items": [
      {
        "id": "3fa85f64-5717-4562-b3fc-2c963f66afa6",
        "name": "string",
        "description": "string",
        "hasImage": true,
        "active": true,
        "imgURL": "string",
        "price": 0,
        "menuId": "3fa85f64-5717-4562-b3fc-2c963f66afa6",
        "childMenuItemId": "3fa85f64-5717-4562-b3fc-2c963f66afa6"
      }
    ]
  }
]
 * 
 * */


class Menu {
  
  String? id;
  String? name;
  String? restaurantId;
  List <Items>? items;
  
  String? get getIdMenu => id;
  String? get getNameMenu => name;
  String? get getRestaurantId => restaurantId;
  List <Items>? get getItemsMenu => items;
  
  Menu({this.id,this.name,this.restaurantId,this.items});
  
  factory Menu.fromJson(Map <String,dynamic> data ){
    
    if(data ["items"] != null){
    var itemsObjs = data ["items"] as List;
    List<Items> _items = itemsObjs.map((itemsObjs) => Items.fromJson(itemsObjs)).toList();
    
    return Menu(
      id : data ["id"],
      name : data ["name"],
      restaurantId : data ["restaurantId"],
      items : _items
    );  
    
    } else {
      return  Menu (
        id : data ["id"],
        name : data ["name"],
        restaurantId : data ["restaurantId"]
      );
    }
  }
  
  @override
  String toString(){
    return '{${this.id},${this.name},${this.restaurantId},${this.items}}';
  }
  
}


class Items {
  
  String? idItems;
  String? nameItems;
  String? descriptionItems;
  bool? hasImageItems;
  bool? activeItems;
  String? imgURLItems;
  int? priceItems;
  String? menuIdItems;
  String? childMenuItemIdItems;
  
  
  Items ({this.idItems, this.nameItems, this.descriptionItems, this.hasImageItems, this.activeItems,
          this.imgURLItems, this.priceItems, this.menuIdItems, this.childMenuItemIdItems});
  
  String? get getIdItems => idItems;
  String? get getNameItems => nameItems;
  String? get getDscriptionItems => descriptionItems;
  bool? get getHasImageItems => hasImageItems;
  bool? get getActiveItems => activeItems;
  String? get getImgURLItems => imgURLItems;
  int? get getPriceItems => priceItems;
  String? get getMenuIdItems => menuIdItems;
  String? get getChildMenuItemIdItems => childMenuItemIdItems;
  
  factory Items.fromJson(Map <String,dynamic> data){
    
    final id = data ["id"];
    final name = data ["name"];
    final description = data ["description"];
    final hasImage = data ["hasImage"];
    final active = data ["active"];
    final imgURL = data ["imgURL"];
    final price = data ["price"];
    final menuId = data ["menuId"];
    final childMenuItemId = data ["childMenuItemId"];
    
    return Items(idItems:id,nameItems:name,descriptionItems:description,hasImageItems:hasImage,activeItems:active,
                 imgURLItems:imgURL,priceItems:price,menuIdItems:menuId,childMenuItemIdItems:childMenuItemId);
    
  }
  
  @override
  String toString(){
    return '{${this.idItems},${this.nameItems},${this.descriptionItems},${this.hasImageItems},${this.hasImageItems},${this.activeItems},${this.imgURLItems},${this.priceItems},${this.menuIdItems},${this.childMenuItemIdItems}}';
  }
  
}

I would like to print "items" nested objects but i can't. Please help, all tutorials that i foud are about parse json but can't find a way to retrive and print the nested objects parsed json. Ty

CodePudding user response:

I change your code and remove the unnecessary code. You can print nested objects like this

void main() {
  var jsonString =
      '{"id":"3fa85f64-5717-4562-b3fc-2c963f66afa6","name":"string","restaurantId":"3fa85f64-5717-4562-b3fc-2c963f66afa6","items":[{"id":"3fa85f64-5717-4562-b3fc-2c963f6xxxxxxxx","name":"string","description":"string","hasImage":true,"active":true,"imgURL":"string","price":0,"menuId":"3fa85f64-5717-4562-b3fc-2c963f66afa6","childMenuItemId":"3fa85f64-5717-4562-b3fc-2c963f66afa6"}]}';
  var parsed = jsonDecode(jsonString);
  Menu menuItems = Menu.fromJson(parsed);
  
  //print menu id
  print(menuItems.getIdMenu);
  //print all items
  print(menuItems.getItemsMenu);
  //print items 0 position id
  print(menuItems.getItemsMenu?[0].getIdItems);
}
  • Related