Flutter - Can't save all json data to Iterable List from api response EDIT: ( Can't Fetch Data from iterable List)
Its actually saving the data. But its not printing on the console when using debugPrint
.
You have to give wrapWidth
in debugPrint
, so that you can see full response stored in a variable.
debugPrint( "=========== Dish_List ==============\n" + list.toString(), wrapWidth: 1000,);debugPrint("====================================");
I think this is the problem you are facing.
You can also do pretty-print to make it understandable
debugPrint( "=========== Dish_List ==============",);JsonEncoder encoder = new JsonEncoder.withIndent(' ');debugPrint('${encoder.convert(list)}', wrapWidth: 1000);debugPrint("====================================");
Listing all the dishes
Container( color: Colors.white, padding: EdgeInsets.fromLTRB(5, 10, 5, 10), child: ListView.builder( itemCount: apiResponse[0]['table_menu_list'][0]['category_dishes'].length, itemBuilder:(cc,ind){ return ListTile( title:Text(apiResponse[0]['table_menu_list'][0]['category_dishes'][ind]['dish_name']) //use other properties as your requirement ); } ))
Getting date from network. I used local assignment of data ! Replace with your own method
void assignData(){ apiResponse = [ { "restaurant_name": "Cafe", "restaurant_image": "http://restaurants.unicomerp.net/images/Restaurant/1010000001.jpg", "table_id": "1", "table_name": "Table 01", "branch_name": "Cafe", "nexturl": "http://snapittapp.snapitt.net/api/menu/10/?org=1010000001&branch_id=1000000001&limit=10&offset=20&lang=en", "table_menu_list": [ { "menu_category": "Salads and Soup", "menu_category_id": "11", "menu_category_image": "http://restaurants.unicomerp.net/images/Restaurant/Item/ItemGroup_11.jpg", "nexturl": "http://snapittapp.snapitt.net/api/menu/20/?org=1010000001&branch_id=1000000001&menuCat=11&limit=10&offset=20&lang=en", "category_dishes": [ { "dish_id": "100000001", "dish_name": "Spinach Salad", "dish_price": 7.95, "dish_image": "http://restaurants.unicomerp.net//images/Restaurant/1010000001/Item/Items/100000001.jpg", "dish_currency": "SAR", "dish_calories": 15, "dish_description": "Fresh spinach, mushrooms, and hard-boiled egg served with warm bacon vinaigrette", "dish_Availability": true, "dish_Type": 2, "nexturl": "http://snapittapp.snapitt.net/api/menu/30/?org=1010000001&branch_id=1000000001&menuItem=100000001&limit=10&offset=20&lang=en", "addonCat": [ { "addon_category": "Spicy/Non-Spicy", "addon_category_id": "104", "addon_selection": 0, "nexturl": "http://snapittapp.snapitt.net/api/menu/40/?org=1010000001&branch_id=1000000001&menuItem=100000001&menuAddonCat=104&menuAddonselc=0&limit=10&offset=20&lang=en", "addons": [ { "dish_id": "100000032", "dish_name": "Non Spicy", "dish_price": 25, "dish_image": "http://restaurants.unicomerp.net/images/Restaurant/Item/Item_100000025.jpg", "dish_currency": "SAR", "dish_calories": 15, "dish_description": "Non Spicy", "dish_Availability": true, "dish_Type": 1 } ] }, { "addon_category": "Add On", "addon_category_id": "101", "addon_selection": 1, "nexturl": "http://snapittapp.snapitt.net/api/menu/40/?org=1010000001&branch_id=1000000001&menuItem=100000001&menuAddonCat=101&menuAddonselc=1&limit=10&offset=20&lang=en", "addons": [ { "dish_id": "100000020", "dish_name": "fried onions", "dish_price": 15, "dish_image": "http://restaurants.unicomerp.net/images/Restaurant/Item/Item_100000020.jpg", "dish_currency": "SAR", "dish_calories": 10, "dish_description": "fried onions", "dish_Availability": true, "dish_Type": 2 } ] } ] }, { "dish_id": "100000003", "dish_name": "Traditional New England Seafood Chowder", "dish_price": 12, "dish_image": "http://restaurants.unicomerp.net/images/Restaurant/1010000001/Item/Items/100000003.jpg", "dish_currency": "SAR", "dish_calories": 30, "dish_description": "with clams, scallops, and shrimp,", "dish_Availability": true, "dish_Type": 1, "nexturl": "http://snapittapp.snapitt.net/api/menu/30/?org=1010000001&branch_id=1000000001&menuItem=100000003&limit=10&offset=20&lang=en", "addonCat": [] }, { "dish_id": "100000004", "dish_name": "Salad Bar Soup", "dish_price": 5, "dish_image": "http://restaurants.unicomerp.net/images/Restaurant/1010000001/Item/Items/100000004.jpg", "dish_currency": "SAR", "dish_calories": 30, "dish_description": "Flour Mixed with fresh green leafy vegetables", "dish_Availability": true, "dish_Type": 2, "nexturl": "http://snapittapp.snapitt.net/api/menu/30/?org=1010000001&branch_id=1000000001&menuItem=100000004&limit=10&offset=20&lang=en", "addonCat": [] }, { "dish_id": "100000005", "dish_name": "chicken-soup", "dish_price": 14.89, "dish_image": "http://restaurants.unicomerp.net/images/Restaurant/1010000001/Item/Items/100000005.jpg", "dish_id": "100000029", "dish_name": "Tacos", "dish_price": 25, "dish_image": "http://restaurants.unicomerp.net/images/Restaurant/Item/Item_100000029.jpg", "dish_currency": "SAR", "dish_calories": 225, "dish_description": "Mexican Street Tacos", "dish_Availability": true, "dish_Type": 3, "nexturl": "http://snapittapp.snapitt.net/api/menu/30/?org=1010000001&branch_id=1000000001&menuItem=100000029&limit=10&offset=20&lang=en", "addonCat": [] } ] } ] }]; }}
The problem is here that you only do it for the first item in result and in table_menu_list
static Resource<List<CategoryDishes>> get all { return Resource( url: Constants.FOOD_API_URL, parse: (response) { final result = json.decode(response.body.toString()); // print(response); Iterable list = result[0]['table_menu_list'][0]['category_dishes']; debugPrint("=========== Dish_List ==============\n" + list.toString()); debugPrint("===================================="); return list.map((model) => CategoryDishes.fromJson(model)).toList(); }); }
The parse function I would do it like this
final result = json.decode(response.body.toString()); // print(response);List<CategoryDishes> list = []List.from(result).forEach((item) =>List.from(item['table_menu_list']).forEach((menuItem) => List.from(menuItem['category_dishes']).forEach((dish) => list.add(CategoryDishes.fromJson(dish)))));debugPrint("=========== Dish_List ==============\n" + list.toString());debugPrint("====================================");return list;
If you don't want to have repeated dishes in the list (dishes with the same id), you could use a map
final result = json.decode(response.body.toString()); // print(response);Map<String, CategoryDishes> map = {}List.from(result).forEach((item) =>List.from(item['table_menu_list']).forEach((menuItem) => List.from(menuItem['category_dishes']).forEach((dish) => map[dish['dish_id']] = CategoryDishes.fromJson(dish);)));return map.values.toList();