Flutter从JSON数组制作GridView

时间:2019-05-30 20:29:40

标签: flutter

有任何解决方案可以使GridView具有这种结构吗?例如?

网格数据为:

{
  'icon':Icons.home,
  'title':'dashboard'
},

我尝试实现此代码

GridView.count(
    crossAxisCount: 4,
    childAspectRatio: 1.0,
    padding: const EdgeInsets.all(4.0),
    mainAxisSpacing: 4.0,
    crossAxisSpacing: 4.0,
    children: <Map<String,String>>[
      {
        'icon':Icons.home,
        'title':'dashboard'
      },
    ].map((Map<String,String> item) {
      return Container(
        child: Column(
          children: <Widget>[
            Icon(IconData(item['icon']))
            Text(item['title']),
          ],
        ),
      );
    }).toList()),

1 个答案:

答案 0 :(得分:0)

需要提供的地图类型为dynamic,例如<Map<String,dynamic>> 而不是<Map<String,String>> StringIconData类型, 因为您的地图数据中包含String和IconData类型的数据,而String无法分配给IconData的类型,反之亦然

GridView.count(
        crossAxisCount: 4,
        childAspectRatio: 1.0,
        padding: const EdgeInsets.all(4.0),
        mainAxisSpacing: 4.0,
        crossAxisSpacing: 4.0,
        children: <Map<String,dynamic>>[
          {
            'icon':Icons.home,
            'title':'dashboard'
          },
        ].map((Map<String,dynamic> item) {
          return Container(
            child: Column(
              children: <Widget>[
                Icon(item['icon']),
                Text(item['title']),
              ],
            ),
          );
        }).toList())

这对您有帮助