【问题标题】:Iterate through map values in dart / flutter迭代飞镖/颤振中的地图值
【发布时间】:2020-05-15 15:46:33
【问题描述】:

我基本上来自 JS 背景,现在正在尝试使用 Flutter 框架。 我一直在格式化从 API 收到的数据。

Data 接收到一个对象数组

 [
   {
     "id": 1,
     "name": "name1",
     "type": "Both",
     "count": 4
   },
   {
     "id": 2,
     "name": "name2",
     "type": "Both",
     "count": 6
   },
   {
     "id": 3,
     "name": "name1",
     "type": "Both",
     "count": 2
   },
   {
     "id": 4,
     "name": "name3",
     "type": "Both",
     "count": 8
   },
   ......
 ]

我的要求是我必须根据名称对数据进行分组

{
  name1: [
           {
             "id": 1, "name":"name1", "type": "Both", "count": 4
           },
           {
             "id": 3, "name":"name1", "type": "Both", "count": 2
           }
  ],
  name2: [
           {
             "id": 2, "name":"name2", "type": "Both", "count": 6
           }
  ],
  .....
}

在 dart 中,我已经设法使用 collections.dart 包中的 groupBy 进行分组。

问题是我无法遍历分组数据。我需要访问计数以对每个分组名称进行一些操作。

以下是我尝试过的代码片段

  Map data;
  List partySnapData;
  Map newMap;

  Future _getTopParties() async {
   final response =await http.get(API_URL + '/getPartySnapShot');
   data =json.decode(response.body);
   partySnapData = data['resultObj'];
   setState(() {
    newMap = groupBy(partySnapData, (obj) => obj['party_name']);
    for(var v in newMap.values) {
     print(v);
    }
 });

}

print(v) 实际上给了我映射到地图 E.G 的值

[
  { "id": 1, "name":"name1", "type": "Both", "count": 4 },
  { "id": 3, "name":"name1", "type": "Both", "count": 2 }
],
.....,
......

现在,我如何循环或迭代数组,这里是 v,以便我可以访问里面的元素?

【问题讨论】:

    标签: flutter dart


    【解决方案1】:

    我找到了解决方案。我发布它,以防它帮助某人。

    我想循环遍历 v 来生成对象。

    newMap = groupBy(partySnapData, (obj) => obj['party_name']);
    for(var v in newMap.values) {
     print(v);
     //below is the solution
      v.asMap().forEach((i, value) {
        print('index=$i, value=$value');
      }
    }
    

    【讨论】:

    【解决方案2】:

    更新

    List<Map> list = [
           {
             "id": 1,
             "name": "name1",
             "type": "Both",
             "count": 4
           },
           {
             "id": 2,
             "name": "name2",
             "type": "Both",
             "count": 6
           },
           {
             "id": 3,
             "name": "name1",
             "type": "Both",
             "count": 2
           },
           {
             "id": 4,
             "name": "name3",
             "type": "Both",
             "count": 8
           }];
            Map sorted={};
    
          @override
          void initState(){
            super.initState();
            for(Map m in list){
              if(sorted[m['name'].toString()]==null)
                sorted[m['name'].toString()]=[];
    
              sorted[m['name'].toString()].add(m);
    
            }
          }
    
          @override
          Widget build(BuildContext context) {
            return Scaffold(
            body:Container(
              padding:EdgeInsets.all(20),
            color:Colors.white,
            alignment:Alignment.center,
            child:Column(
              mainAxisSize:MainAxisSize.min,
              children: <Widget>[
                Text('Original\n $list',
                          style:TextStyle(color:Colors.black)),
                SizedBox(height:15),
                Text('Sorted\n $sorted',
                          style:TextStyle(color:Colors.black)),
    //EDIT
                SizedBox(height:15),
                ListView(
                  shrinkWrap:true,
                  children: sorted.entries.map<Widget>((value){
                    return Column(
                         mainAxisSize:MainAxisSize.min,
                         crossAxisAlignment:CrossAxisAlignment.start,
                         children:[
    
    Text(value.key.toString(),style:TextStyle(color:Colors.black,
                                                         fontWeight:FontWeight.bold)),
                Column(
                mainAxisSize:MainAxisSize.min,
                children:value.value.map<Widget>((subValue){
                  return Text(subValue['count'].toString(),style:TextStyle(color:Colors.black));
                }).toList(),
                ),
              ]
              );
            }).toList(),
           )
              ],
            )
            )
            );
          }
    

    【讨论】:

    • 感谢您的回复,但我认为,我需要知道如何循环通过排序数组来访问键,例如计数。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-06-26
    • 2020-08-12
    • 2020-12-25
    • 2020-11-05
    • 2023-03-05
    • 2021-06-07
    • 1970-01-01
    相关资源
    最近更新 更多