遍历飞镖/颤振中的贴图值

时间:2020-01-30 04:23:51

标签: flutter dart

我基本上是从JS背景开始的,现在尝试使用Flutter框架。 我无法格式化从API接收到的数据。

数据接收到对象数组

 [
   {
     "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 ),以便可以访问其中的元素?

2 个答案:

答案 0 :(得分:5)

我找到了解决方案。我发布是为了帮助别人。

我想遍历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');
  }
}

答案 1 :(得分:0)

已更新

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(),
       )
          ],
        )
        )
        );
      }

SS