我有以下要加载到Flutter应用程序中的JSON数据:
{
"Games": [
{
"name": "Sample Name",
"categories": [
{
"name": "Category 1",
"multiplier": 1,
"order": 2
},
{
"name": "Category 2",
"order": 9,
"Range": [
{
"ceiling": 0,
"floor": 0,
"score": 0
},
{
"ceiling": 1,
"floor": 1,
"score": 2
}
]
},
{
"name": "Category 4",
"multiplier": -1,
"order": 11,
"penalty": true
},
{
"name": "Category 5",
"multiplier": 1,
"order": 8
},
{
"name": "Category 6",
"multiplier": -1,
"order": 10,
"penalty": true
}
],
"enabled": true,
"maxPlayers": 4,
"minPlayers": 1
}
]
}
我正在使用以下代码从上面的地图填充对象:
class Entry {
String name;
// final String filename;
int minPlayers;
int maxPlayers;
bool enabled;
bool selected = false;
DocumentReference reference;
List<Category> categories;
Entry.fromMap(Map<String, dynamic> map, {this.reference}) {
name = map['name'];
enabled = map['enabled'];
minPlayers = map['minPlayers'];
maxPlayers = map['maxPlayers'];
categories =
(map['categories'].map((data) => Category.fromMap(data)).toList());
}
class Category {
String name;
int order;
int multiplier;
bool penalty;
Category.fromMap(Map<String, dynamic> map, {DocumentReference reference}) {
name = map['name'];
order = map['order'];
multiplier = map['multiplier'];
penalty = !map.containsKey('penalty');
}
Category.fromSnapshot(DocumentSnapshot snapshot)
: this.fromMap(snapshot.data, reference: snapshot.reference);
}
运行它时,出现以下错误:
type '_InternalLinkedHashMap<dynamic, dynamic>' is not a subtype of type 'Map<String, dynamic>'
如何将类别元素的集合放入可迭代的对象列表中,以容纳每个类别,然后从map ['categories']中的单个元素加载该类别?
答案 0 :(得分:0)
我稍微重构了您的代码,但由于我无权访问其代码,因此删除了该引用。 我试过了,效果很好
class Entry{
String name;
// final String filename;
int minPlayers;
int maxPlayers;
bool enabled;
bool selected;
List<Category> categories;
Entry({
this.name,
this.minPlayers,
this.maxPlayers,
this.enabled,
this.categories,
this.selected =false
});
factory Entry.fromMap(Map<String, dynamic> map) {
return Entry(
name: map['name'],
enabled: map['enabled'],
minPlayers: map['minPlayers'],
maxPlayers: map['maxPlayers'],
categories: Category.fromList(map['categories'])
);
}
static List<Entry> fromList(List json) {
return json.map((entry) => Entry.fromMap(entry)).toList();
}
}
class Category {
String name;
int order;
int multiplier;
bool penalty;
Category(
{
this.name,
this.order,
this.multiplier,
this.penalty
}
);
factory Category.fromMap(Map<String, dynamic> map) {
return Category(
name :map['name'],
order : map['order'],
multiplier : map['multiplier'],
penalty : map['penalty'] ?? false
);
}
static List<Category> fromList(List json) {
return json.map((category) => Category.fromMap(category)).toList();
}
}
并解码您提供的示例,请使用:
List<Entry> entries = Entry.fromList(test["Games"]);
答案 1 :(得分:0)
我认为您尚未为各自的json创建有效的dart类。您的json具有类似Entry类的体系结构,其中包含多个游戏,
每个游戏都有多个类别,
每个类别具有多个范围。
请使用this工具将json转换为Dart类
我已经为您的json创建了模型类,请检查this链接。
现在该解析了。我只是将欢迎班改名为Entry。
解析
final jsonResponse = json.decode('YOUR JSON STRING');
Entry entry =Entry.fromJson(jsonResponse);
print(entry.games.length);
entry.games.map((game) {
print("Game Name " + game.name + "----------");
print("Category List");
game.categories.map((category) {
print(category.name);
}).toList();
}).toList();
现在检查调试控制台。所有类别和游戏显示。