【发布时间】:2023-03-18 09:09:01
【问题描述】:
我正在从 api 检索数据映射,如下所示
{
"success": true,
"data": {
"items": [
{
"sequenceno": 1933,
"_id": "5eff1",
"chapter": "Numbers and Numeration",
"title": "Place Value: 3-digits",
"package_description": "This learning module helps to familiarise with the concept of place value of 3-digit numbers.",
"age_level": [
99,
8
],
"pkg_sequence": "2501",
"packagescors": {
"score": [
50
],
"date": [
"1600259121340"
]
}
},
{
"sequenceno": 1933,
"_id": "5d79",
"chapter": "Numbers and Numeration",
"title": "Place Value: 4-digits",
"package_description": "This learning module helps the kids familiarise with the concept of Place value of a number.",
"age_level": [
99,
8
],
"pkg_sequence": "2501",
"packagescors": {
"score": [
60
],
"date": [
"1615283866457"
]
}
},
]
我的目标是使用关键的“章节”对它们进行排序和分组。如下所示
Numbers and Numeration : { // chapter name
"sequenceno": 1933,
"_id": "5d79",
"chapter": "Numbers and Numeration",
"title": "Place Value: 4-digits",
"package_description": "This learning module helps the kids familiarise with the concept of Place value of a number.",
"age_level": [
99,
8
],
"pkg_sequence": "2501",
"packagescors": {
"score": [
60
],
"date": [
"1615283866457"
]
{
"sequenceno": 1933,
"_id": "5eff1",
"chapter": "Numbers and Numeration",
"title": "Place Value: 3-digits",
"package_description": "This learning module helps to familiarise with the concept of place value of 3-digit numbers.",
"age_level": [
99,
8
],
"pkg_sequence": "2501",
"packagescors": {
"score": [
50
],
"date": [
"1600259121340"
]
}
}
与此类似,还有多个章节。那么如何对它们进行分组,以便章节名称在前,然后章节的所有相关值都在该组下?获取值的正确方法是什么
** 更新:-我正在尝试使用 for 循环来遍历数据。我想要做的是,如果密钥存在,则相应地对数据进行分组以添加密钥
Map<Item,dynamic> sortedTable = {};
Iterable<Map<String, dynamic>> data = subjectModules.map((e) => e.toMap());
for (var map in data) {
try{
if(sortedTable.containsKey(map['chapter'])){ // if key exists
// need to add and data group it
}else{ // if not
sortedTable.putIfAbsent(map['chapter'], () => map); // i need add only keys
}
}catch(e){
print('Error $e');
}
}
如何正确迭代和分组?
【问题讨论】:
标签: flutter for-loop dart fluttermap