Flutter List 不是“Iterable<dynamic>”类型的子类型
Posted
技术标签:
【中文标题】Flutter List 不是“Iterable<dynamic>”类型的子类型【英文标题】:Flutter List is not a subtype of type 'Iterable<dynamic>' 【发布时间】:2020-06-17 17:41:50 【问题描述】:我是 Flutter 开发的新手。我收到错误
List 不是“Iterable”类型的子类型
到目前为止,我已经这样做了。
模型类
class Categories
final String title;
final Items items;
Categories(this.title, this.items);
factory Categories.fromJson(Map<String, dynamic> json)
return new Categories(
title: json['title'],
items: Items.fromJson(json['Items']));
class Items
final String tag;
final String childId;
final String category;
final String isNew;
Items(this.tag, this.childId, this.category, this.isNew);
factory Items.fromJson(Map<String, dynamic> json)
return new Items(
tag: json['id'],
childId: json['child_category_id'],
category: json['category'],
isNew: json['new']);
缅因州班
List<Categories> categories = [];
var loading = false;
Future<Null> getNavigationItems() async
setState(()
loading = true;
);
final response = await http.get(Uri.encodeFull(APIs.url_getCategory2));
if (response.statusCode == 200)
final data = jsonDecode(utf8.decode(response.bodyBytes));
setState(()
for(Map i in data)
categories.add(Categories.fromJson(i));
loading = false;
);
我正在尝试从 2 天内解决问题。
这是我从服务器端获取的 JSON 格式
这是在扩展它的第一个节点之后
更新图片
请帮忙,谢谢。
这里是 JSON 响应,您可以查看 Link
【问题讨论】:
setState(() for(Map i in data) categories.add(Categories.fromJson(i)); 在这一行我收到错误 _TypeError(类型 '_InternalLinkedHashMap您是否尝试过使用var
而不是Map
?
for (var i in data)
categories.add(Categories.fromJson(i));
【讨论】:
不工作。得到相同的错误 - 发生异常。 _TypeError(类型 '_InternalLinkedHashMap首先,您无法使用 json["title"] 获取键 SOFAR 作为标题。我已经重写了,让我们看看:
Map<String, dynamic> categoriesJson = data["categories"];
// get all category name with this because you can't get the key name with "title"
List<String> categoriesName = categoriesJson.keys.map((key)
return key.toString();
).toList();
// depend on the key name, rewrite new Map and put the key name and Items to it
for (String categoryName in categoriesName)
Map<String, dynamic> category =
"title": categoriesName,
"Items": categoriesJson[categoriesName]["Items"]
;
categories.add(Categories.fromJson(category));
【讨论】:
现在这个错误 NoSuchMethodError (NoSuchMethodError: The method '[]' was called on null. Receiver: null 尝试调用: []("Items")) 在“for in”的第一行添加 print(categoryName) 并显示给我 print(categoryName) 给出了这个 ---> 猫名:[沙发、客厅、卧室、餐厅、存储、书房、儿童家具、装饰] 可以在 Map只需查看修改后的代码,我已经根据动态 JSON 对象创建了一个列表。
"icons":
"homeIcon": "https:\/\/www.woodenstreet.com\/images\/mobileapp\/drawericon\/home.png",
"cusomIcon": "https:\/\/www.woodenstreet.com\/images\/mobileapp\/drawericon\/custom.png",
"looks": "https:\/\/www.woodenstreet.com\/images\/mobileapp\/drawericon\/look-book.png",
"policyIcon": "https:\/\/www.woodenstreet.com\/images\/mobileapp\/drawericon\/privacy-policy.png",
"interiorIcon": "https:\/\/www.woodenstreet.com\/images\/mobileapp\/drawericon\/interior.png",
"emailIcon": "https:\/\/www.woodenstreet.com\/images\/mobileapp\/drawericon\/newsletter.png",
"aboutusIcon": "https:\/\/www.woodenstreet.com\/images\/mobileapp\/drawericon\/about-us.png"
,
"categories":
"SOFAS":
"icon": "https:\/\/www.woodenstreet.com\/images\/mobileapp\/drawericon\/sofa.png",
"Items": [
"tag": "parent",
"child_category_id": "10",
"category": "SOFA SETS",
"new": "0"
,
"tag": "child",
"child_category_id": "11",
"category": "Fabric Sofas",
"new": "1"
,
"tag": "child",
"child_category_id": "12",
"category": "Wooden Sofas",
"new": "1"
,
"tag": "child",
"child_category_id": "298",
"category": "3 Seater Sofas",
"new": "0"
,
"tag": "child",
"child_category_id": "299",
"category": "2 Seater Sofas",
"new": "0",
"icon": ""
,
"tag": "child",
"child_category_id": "666",
"category": "L Shaped Corner Sofas",
"new": "0"
,
"tag": "child",
"child_category_id": "312",
"category": "Chesterfield Sofas",
"new": "0"
,
"tag": "child",
"child_category_id": "1079",
"category": "Recliners",
"new": "0"
,
"tag": "child",
"child_category_id": "120",
"category": "Futons",
"new": "0"
,
"tag": "parent",
"child_category_id": "13",
"category": "SOFA CUM BED",
"new": "0"
,
"tag": "child",
"child_category_id": "108",
"category": "Wooden Sofa Cum Beds",
"new": "1"
,
"tag": "child",
"child_category_id": "107",
"category": "Fabric Sofa Cum Beds",
"new": "0"
,
"tag": "child",
"child_category_id": "13",
"category": "Sofa Cum Beds",
"new": "0"
,
"tag": "parent",
"child_category_id": "",
"category": "SEATING",
"new": "0"
,
"tag": "child",
"child_category_id": "124",
"category": "Wingback Chairs",
"new": "1"
,
"tag": "child",
"child_category_id": "36",
"category": "Lounge Chairs",
"new": "0"
,
"tag": "child",
"child_category_id": "40",
"category": "Chaise Lounge",
"new": "0"
,
"tag": "child",
"child_category_id": "126",
"category": "Arm Chair",
"new": "0"
,
"tag": "child",
"child_category_id": "42",
"category": "Stools",
"new": "0"
,
"tag": "child",
"child_category_id": "116",
"category": "Divans",
"new": "1"
,
"tag": "child",
"child_category_id": "77",
"category": "Ottomans",
"new": "1"
,
"tag": "child",
"child_category_id": "118",
"category": "Loveseats",
"new": "1"
]
,
"LIVING":
"icon": "https:\/\/www.woodenstreet.com\/images\/mobileapp\/drawericon\/living.png",
"Items": [
"tag": "parent",
"child_category_id": "16",
"category": "LIVING STORAGE",
"new": "0"
,
"tag": "child",
"child_category_id": "17",
"category": "TV Units",
"new": "1"
,
"tag": "child",
"child_category_id": "22",
"category": "Bookshelves",
"new": "0"
,
"tag": "child",
"child_category_id": "20",
"category": "Display Unit",
"new": "0"
,
"tag": "child",
"child_category_id": "19",
"category": "Shoe Racks",
"new": "1"
,
"tag": "child",
"child_category_id": "18",
"category": "Wall Shelves",
"new": "0"
,
"tag": "child",
"child_category_id": "21",
"category": "Prayer Units",
"new": "0"
,
"tag": "child",
"child_category_id": "23",
"category": "Magazine Racks",
"new": "0"
,
"tag": "parent",
"child_category_id": "24",
"category": "TABLES",
"new": "0"
,
"tag": "child",
"child_category_id": "25",
"category": "Coffee Tables",
"new": "1"
,
"tag": "child",
"child_category_id": "134",
"category": "Coffee Table Sets",
"new": "1"
,
"tag": "child",
"child_category_id": "27",
"category": "Nest of Tables",
"new": "0"
,
"tag": "child",
"child_category_id": "135",
"category": "Side & End Tables",
"new": "0"
,
"tag": "child",
"child_category_id": "26",
"category": "Console Table",
"new": "0"
,
"tag": "parent",
"child_category_id": "116",
"category": "DIVANS",
"new": "0"
,
"tag": "child",
"child_category_id": "116",
"category": "Divans",
"new": "0"
,
"tag": "parent",
"child_category_id": "38",
"category": "CHAIRS",
"new": "0"
,
"tag": "child",
"child_category_id": "36",
"category": "Lounge Chairs",
"new": "0"
,
"tag": "child",
"child_category_id": "124",
"category": "Wing Chairs",
"new": "0"
,
"tag": "child",
"child_category_id": "35",
"category": "Rocking Chairs",
"new": "0"
,
"tag": "child",
"child_category_id": "126",
"category": "Arm Chairs",
"new": "0"
,
"tag": "child",
"child_category_id": "130",
"category": "Metal Chairs",
"new": "0"
,
"tag": "child",
"child_category_id": "37",
"category": "Kids Chairs",
"new": "0"
,
"tag": "child",
"child_category_id": "42",
"category": "Stools",
"new": "0"
,
"tag": "child",
"child_category_id": "43",
"category": "Benches",
"new": "0"
,
"tag": "child",
"child_category_id": "122",
"category": "Bean Bags",
"new": "0"
,
"tag": "parent",
"child_category_id": "5",
"category": "OUTDOOR",
"new": "0"
,
"tag": "child",
"child_category_id": "98",
"category": "Garden Furniture",
"new": "0"
,
"tag": "child",
"child_category_id": "96",
"category": "Balcony Furniture",
"new": "0"
,
"tag": "child",
"child_category_id": "138",
"category": "Wooden Swings",
"new": "0"
,
"tag": "parent",
"child_category_id": "673",
"category": "OFFICE FURNITURE",
"new": "0"
,
"tag": "child",
"child_category_id": "681",
"category": "Office Tables",
"new": "0"
,
"tag": "child",
"child_category_id": "674",
"category": "Office Chairs",
"new": "0"
]
,
"BEDROOM":
"icon": "https:\/\/www.woodenstreet.com\/images\/mobileapp\/drawericon\/bedroom.png",
"Items": [
"tag": "parent",
"child_category_id": "55",
"category": "BEDS",
"new": "0"
,
"tag": "child",
"child_category_id": "59",
"category": "Queen Size Beds",
"new": "0"
,
"tag": "child",
"child_category_id": "61",
"category": "King Size Beds",
"new": "0"
,
"tag": "child",
"child_category_id": "58",
"category": "Single Beds",
"new": "0"
,
"tag": "child",
"child_category_id": "149",
"category": "Hydraulic Storage Beds",
"new": "0"
,
"tag": "child",
"child_category_id": "150",
"category": "Upholstered Beds",
"new": "1"
,
"tag": "child",
"child_category_id": "13",
"category": "Sofa cum Beds",
"new": "0"
,
"tag": "child",
"child_category_id": "151",
"category": "Trundle Beds",
"new": "1"
,
"tag": "child",
"child_category_id": "60",
"category": "Double Beds",
"new": "0"
,
"tag": "child",
"child_category_id": "62",
"category": "Poster Beds",
"new": "0"
,
"tag": "child",
"child_category_id": "116",
"category": "Divan Beds",
"new": "0"
,
"tag": "parent",
"child_category_id": "65",
"category": "BEDROOM STORAGE & ACCESSORIES",
"new": "0"
,
"tag": "child",
"child_category_id": "67",
"category": "Bedside Table",
"new": "0"
,
"tag": "child",
"child_category_id": "101",
"category": "Chest of Drawers",
"new": "0"
,
"tag": "child",
"child_category_id": "91",
"category": "Wardrobes",
"new": "0"
,
"tag": "child",
"child_category_id": "68",
"category": "Dressing Table",
"new": "0"
,
"tag": "child",
"child_category_id": "154",
"category": "Trunk & Blanket Boxes",
"new": "0"
,
"tag": "child",
"child_category_id": "66",
"category": "Breakfast Table",
"new": "0"
,
"tag": "child",
"child_category_id": "18",
"category": "Wall Shelves",
"new": "0"
,
"tag": "child",
"child_category_id": "178",
"category": "Room Dividers",
"new": "0"
,
"tag": "parent",
"child_category_id": "74",
"category": "Mattress & Bedding",
"new": "0"
,
"tag": "child",
"child_category_id": "548",
"category": "Mattresses",
"new": "0"
,
"tag": "child",
"child_category_id": "549",
"category": "Single Bed Mattress",
"new": "0"
,
"tag": "child",
"child_category_id": "551",
"category": "Queen Size mattress",
"new": "0"
,
"tag": "child",
"child_category_id": "550",
"category": "King Size Mattress",
"new": "0"
,
"tag": "child",
"child_category_id": "552",
"category": "Double Bed Mattress",
"new": "0"
,
"tag": "parent",
"child_category_id": "155",
"category": "KIDS BEDROOM",
"new": "0"
,
"tag": "child",
"child_category_id": "64",
"category": "Bunk Beds",
"new": "0"
,
"tag": "child",
"child_category_id": "63",
"category": "Kids Beds",
"new": "0"
,
"tag": "child",
"child_category_id": "156",
"category": "Cribs",
"new": "0"
,
"tag": "child",
"child_category_id": "31",
"category": "Kids Study Table",
"new": "0"
,
"tag": "child",
"child_category_id": "37",
"category": "Kids Chairs",
"new": "0"
]
,
"DINING":
"icon": "https:\/\/www.woodenstreet.com\/images\/mobileapp\/drawericon\/dining.png",
"Items": [
"tag": "parent",
"child_category_id": "44",
"category": "DINING ROOM FURNITURE",
"new": "0"
,
"tag": "child",
"child_category_id": "47",
"category": "6 Seater Dining Sets",
"new": "1"
,
"tag": "child",
"child_category_id": "46",
"category": "4 Seater Dining Sets",
"new": "0"
,
"tag": "child",
"child_category_id": "49",
"category": "2 Seater Dining Sets",
"new": "0"
,
"tag": "child",
"child_category_id": "51",
"category": "Extendable Dining Sets",
"new": "0"
,
"tag": "child",
"child_category_id": "143",
"category": "Round Dining Sets",
"new": "0"
,
"tag": "child",
"child_category_id": "48",
"category": "Dining Tables",
"new": "0"
,
"tag": "child",
"child_category_id": "141",
"category": "8 Seater Dining Sets",
"new": "1"
,
"tag": "child",
"child_category_id": "44",
"category": "Dining Table Sets",
"new": "0"
,
"tag": "parent",
"child_category_id": "383",
"category": "DINING CHAIRS",
"new": "0"
,
"tag": "child",
"child_category_id": "383",
"category": "Dining Chairs",
"new": "0"
,
"tag": "child",
"child_category_id": "43",
"category": "Benches",
"new": "0"
,
"tag": "parent",
"child_category_id": "11",
"category": "KITCHEN FURNITURE",
"new": "0"
,
"tag": "child",
"child_category_id": "54",
"category": "Cabinets & Sideboards",
"new": "0"
,
"tag": "child",
"child_category_id": "177",
"category": "Kitchen Cabinets",
"new": "1"
,
"tag": "child",
"child_category_id": "190",
"category": "Kitchen Trolley",
"new": "1"
,
"tag": "child",
"child_category_id": "517",
"category": "Kitchen Island",
"new": "1"
,
"tag": "child",
"child_category_id": "540",
"category": "Crockery Unit",
"new": "1"
,
"tag": "parent",
"child_category_id": "11",
"category": "BAR FURNITURE",
"new": "0"
,
"tag": "child",
"child_category_id": "53",
"category": "Bar Cabinets",
"new": "0"
,
"tag": "child",
"child_category_id": "147",
"category": "Bar Trolleys",
"new": "0"
,
"tag": "child",
"child_category_id": "148",
"category": "Wine Racks",
"new": "0"
,
"tag": "child",
"child_category_id": "41",
"category": "Bar Stools & Chairs",
"new": "0"
]
,
"STORAGE":
"icon": "https:\/\/www.woodenstreet.com\/images\/mobileapp\/drawericon\/stores.png",
"Items": [
"tag": "parent",
"child_category_id": "16",
"category": "LIVING STORAGE",
"new": "0"
,
"tag": "child",
"child_category_id": "17",
"category": "TV Units",
"new": "1"
,
"tag": "child",
"child_category_id": "22",
"category": "Bookshelves",
"new": "1"
,
"tag": "child",
"child_category_id": "20",
"category": "Display Units",
"new": "0"
,
"tag": "child",
"child_category_id": "19",
"category": "Shoe Racks",
"new": "1"
,
"tag": "child",
"child_category_id": "18",
"category": "Wall Shelves",
"new": "0"
,
"tag": "child",
"child_category_id": "21",
"category": "Prayer Units",
"new": "0"
,
"tag": "child",
"child_category_id": "23",
"category": "Magazine Racks",
"new": "0"
,
"tag": "parent",
"child_category_id": "100",
"category": "BEDROOM STORAGE",
"new": "0"
,
"tag": "child",
"child_category_id": "101",
"category": "Chest of Drawers",
"new": "0"
,
"tag": "child",
"child_category_id": "91",
"category": "Wardrobes",
"new": "0"
,
"tag": "child",
"child_category_id": "67",
"category": "Bedside Tables",
"new": "0"
,
"tag": "child",
"child_category_id": "154",
"category": "Trunk & Blanket Boxes",
"new": "0"
,
"tag": "child",
"child_category_id": "66",
"category": "Breakfast Table",
"new": "0"
,
"tag": "child",
"child_category_id": "68",
"category": "Dressing Table",
"new": "0"
,
"tag": "child",
"child_category_id": "18",
"category": "Wall Shelves",
"new": "0"
,
"tag": "parent",
"child_category_id": "11",
"category": "DINING STORAGE",
"new": "0"
,
"tag": "child",
"child_category_id": "54",
"category": "Cabinets & Sideboards",
"new": "0"
,
"tag": "child",
"child_category_id": "177",
"category": "Kitchen Cabinets",
"new": "1"
,
"tag": "child",
"child_category_id": "190",
"category": "Kitchen Trolley",
"new": "1"
,
"tag": "child",
"child_category_id": "517",
"category": "Kitchen Island",
"new": "1"
,
"tag": "child",
"child_category_id": "540",
"category": "Crockery Unit",
"new": "1"
,
"tag": "child",
"child_category_id": "144",
"category": "Hutch Cabinets",
"new": "0"
,
"tag": "parent",
"child_category_id": "160",
"category": "BATHROOM STORAGE",
"new": "0"
,
"tag": "child",
"child_category_id": "112",
"category": "Bathroom Cabinets",
"new": "0"
,
"tag": "parent",
"child_category_id": "145",
"category": "BAR FURNITURE",
"new": "0"
,
"tag": "child",
"child_category_id": "53",
"category": "Bar Cabinets",
"new": "0"
,
"tag": "child",
"child_category_id": "147",
"category": "Bar Trolleys",
"new": "0"
,
"tag": "child",
"child_category_id": "148",
"category": "Wine Racks",
"new": "0"
,
"tag": "child",
"child_category_id": "41",
"category": "Bar Stools & Chairs",
"new": "0"
]
,
"STUDY":
"icon": "https:\/\/www.woodenstreet.com\/images\/mobileapp\/drawericon\/study.png",
"Items": [
"tag": "parent",
"child_category_id": "24",
"category": "TABLES",
"new": "0"
,
"tag": "child",
"child_category_id": "29",
"category": "Study Tables",
"new": "0"
,
"tag": "child",
"child_category_id": "30",
"category": "Laptop Tables",
"new": "0"
,
"tag": "child",
"child_category_id": "31",
"category": "Kids Study Tables",
"new": "0"
,
"tag": "child",
"child_category_id": "32",
"category": "Computer Tables",
"new": "0"
,
"tag": "parent",
"child_category_id": "11",
"category": "CHAIRS",
"new": "0"
,
"tag": "child",
"child_category_id": "37",
"category": "Kids Study Chairs",
"new": "0"
,
"tag": "child",
"child_category_id": "159",
"category": "Study Chairs",
"new": "0"
,
"tag": "parent",
"child_category_id": "11",
"category": "STORAGE",
"new": "0"
,
"tag": "child",
"child_category_id": "18",
"category": "Wall Shelves",
"new": "0"
,
"tag": "child",
"child_category_id": "22",
"category": "Bookshelves",
"new": "0"
,
"tag": "child",
"child_category_id": "1078",
"category": "Table Organizer",
"new": "0"
]
,
"KIDS FURNITURE":
"icon": "https:\/\/www.woodenstreet.com\/images\/mobileapp\/drawericon\/kids.png",
"Items": [
"tag": "parent",
"child_category_id": "155",
"category": "KIDS BEDROOM",
"new": "0"
,
"tag": "child",
"child_category_id": "63",
"category": "Kids Beds",
"new": "1"
,
"tag": "child",
"child_category_id": "64",
"category": "Bunk Beds",
"new": "0"
,
"tag": "child",
"child_category_id": "156",
"category": "Cribs",
"new": "0"
,
"tag": "child",
"child_category_id": "157",
"category": "Kids Storage",
"new": "0"
,
"tag": "parent",
"child_category_id": "293",
"category": "KIDS STUDY ROOM",
"new": "0"
,
"tag": "child",
"child_category_id": "37",
"category": "Kids Chairs",
"new": "0"
,
"tag": "child",
"child_category_id": "31",
"category": "Kids Study Tables",
"new": "0"
]
,
"DECOR":
"icon": "https:\/\/www.woodenstreet.com\/images\/mobileapp\/drawericon\/decor.png",
"Items": [
"tag": "parent",
"child_category_id": "74",
"category": "LAMPS & LIGHTING",
"new": "0"
,
"tag": "child",
"child_category_id": "573",
"category": "Table Lamps",
"new": "0"
,
"tag": "child",
"child_category_id": "1084",
"category": "Floor Lamps",
"new": "0"
,
"tag": "child",
"child_category_id": "558",
"category": "Study Lamps",
"new": "0"
,
"tag": "child",
"child_category_id": "557",
"category": "Tripod Lamps",
"new": "0"
,
"tag": "child",
"child_category_id": "71",
"category": "Hanging Lights",
"new": "0"
,
"tag": "parent",
"child_category_id": "176",
"category": "Home Furnishing",
"new": "0"
,
"tag": "child",
"child_category_id": "176",
"category": "Rugs And Carpets",
"new": "0"
,
"tag": "child",
"child_category_id": "521",
"category": "Cushions",
"new": "0"
,
"tag": "child",
"child_category_id": "547",
"category": "Cushion Covers",
"new": "0"
,
"tag": "child",
"child_category_id": "506",
"category": "Curtains",
"new": "0"
,
"tag": "child",
"child_category_id": "566",
"category": "Bed Sheets",
"new": "0"
,
"tag": "child",
"child_category_id": "567",
"category": "Quilts",
"new": "0"
,
"tag": "child",
"child_category_id": "1094",
"category": "Runners",
"new": "0"
,
"tag": "parent",
"child_category_id": "73",
"category": "Photo Frames",
"new": "0"
,
"tag": "child",
"child_category_id": "73",
"category": "Photo Frames",
"new": "0"
,
"tag": "child",
"child_category_id": "1085",
"category": "Single Photo Frames",
"new": "0"
,
"tag": "child",
"child_category_id": "1086",
"category": "Multi Photo Frames",
"new": "0"
,
"tag": "child",
"child_category_id": "1087",
"category": "Collage Photo Frames",
"new": "0"
,
"tag": "parent",
"child_category_id": "74",
"category": "Decor Accessories",
"new": "0"
,
"tag": "child",
"child_category_id": "579",
"category": "Artificial Flowers",
"new": "0"
,
"tag": "child",
"child_category_id": "411",
"category": "Artificial Plants",
"new": "0"
,
"tag": "child",
"child_category_id": "594",
"category": "Miniatures",
"new": "0"
,
"tag": "child",
"child_category_id": "1089",
"category": "Coasters",
"new": "0"
,
"tag": "child",
"child_category_id": "80",
"category": "Figurines",
"new": "0"
,
"tag": "parent",
"child_category_id": "74",
"category": "Wall Art",
"new": "0"
,
"tag": "child",
"child_category_id": "1093",
"category": "Metal Wall Art",
"new": "0"
,
"tag": "child",
"child_category_id": "580",
"category": "Wall Frames",
"new": "0"
,
"tag": "child",
"child_category_id": "873",
"category": "Wooden Jharokha",
"new": "0"
,
"tag": "child",
"child_category_id": "74",
"category": "Mirror Frames",
"new": "0"
,
"tag": "child",
"child_category_id": "1091",
"category": "Wall Tiles",
"new": "0"
,
"tag": "child",
"child_category_id": "1092",
"category": "Wall Clocks",
"new": "0"
,
"tag": "child",
"child_category_id": "1090",
"category": "Wall Murals",
"new": "0"
]
这是我创建的用于存储动态数据的模型类。
class Categories
String itemName;
List<Item> items;
Categories(
this.itemName,
this.items,
);
class Item
String tag;
String childCategoryId;
String category;
String itemNew;
Item(
this.tag,
this.childCategoryId,
this.category,
this.itemNew,
);
factory Item.fromJson(Map<String, dynamic> json) => Item(
tag: json["tag"],
childCategoryId: json["child_category_id"],
category: json["category"],
itemNew: json["new"],
);
Map<String, dynamic> toJson() =>
"tag": tag,
"child_category_id": childCategoryId,
"category": category,
"new": itemNew,
;
这是获取动态数据的主文件
import 'dart:convert';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:sample_project_for_api/NewModel.dart';
import 'package:sample_project_for_api/model.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget
@override
Widget build(BuildContext context)
return MaterialApp(
debugShowCheckedModeBanner: false,
title: 'Flutter Demo',
home: MyHomePage(title: 'Flutter Demo Home Page'),
);
class MyHomePage extends StatefulWidget
MyHomePage(Key key, this.title) : super(key: key);
final String title;
@override
_MyHomePageState createState() => _MyHomePageState();
class _MyHomePageState extends State<MyHomePage>
MyModel yourObject = new MyModel();
bool _isLoading = false;
@override
void initState()
super.initState();
yourMethod();
yourMethod() async
setState(()
_isLoading = true;
);
String jsonString = await loadFromAssets();
Map newStringMap = json.decode(jsonString);
List<Categories> categoryList = new List();
List<Item> listItem = new List();
newStringMap['categories'].forEach((key, value)
listItem = List<Item>.from(value["Items"].map((x) => Item.fromJson(x)));
Categories categories = new Categories(itemName: key, items: listItem);
categoryList.add(categories);
);
for (int i = 0; i < categoryList.length; i++)
print(
'This is the item name ############# : $categoryList[i].itemName');
for (int j = 0; j < categoryList[i].items.length; j++)
print(
'This is the item length --------- :$categoryList[i].items[j].category');
setState(()
_isLoading = false;
);
Future<String> loadFromAssets() async
return await rootBundle.loadString('json/parse.json');
@override
Widget build(BuildContext context)
return Scaffold(
appBar: AppBar(
title: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: <Widget>[Icon(Icons.ac_unit), Icon(Icons.access_alarm)],
)),
body: Text('sample text'),
);
让我知道它是否有效。
【讨论】:
所有这些“LIVING”、“SOFAS”、“BEDROOM”对象都是动态的,有时会出现,有时不会。所以我不能在模型类中添加它作为固定变量。 是的,我做到了,现在如何将这些值设置为 listview 在上面提到的图像中,如果您创建了 ListView,只需将列表传递给项目计数参数。如果您遇到问题,只需插入侧面导航的代码,这样我就可以看到出了什么问题。以上是关于Flutter List 不是“Iterable<dynamic>”类型的子类型的主要内容,如果未能解决你的问题,请参考以下文章
Flutter 小技巧之 Dart 里的 List 和 Iterable 你真的搞懂了吗?
Flutter - 无法将所有 json 数据从 api 响应保存到 Iterable List 编辑:(无法从可迭代列表中获取数据)
“Iterable<Element> 不能转换为 List<Element>” - `List` 不是 `Iterable` 的一种吗?
Flutter Json 未处理异常:类型“_InternalLinkedHashMap<String, dynamic>”不是“Iterable<dynamic>”类型的子类