代码之家  ›  专栏  ›  技术社区  ›  Irfan Ganatra

如何在flutter中的列表视图生成器中使用映射的键和值

  •  0
  • Irfan Ganatra  · 技术社区  · 2 年前

    我创建了一个演示来理解Map

    这里我有一张地图<字符串,双>并希望制作一个列表视图,其中卡片显示密钥和值

    这是我的代码

     Widget build(BuildContext context) {
        Map<String,double> mymap={'Provision':6300,'Food':3230,'shopping':5039,'petrol':1323};
    
        return ListView.builder(
            itemCount: mymap.length,
            itemBuilder: (context,index){
              
              return Card(
                child: Container(
                    height: 100,
                    child: Column(
                      mainAxisSize: MainAxisSize.min,
                      children: [
                      Text('here i want to print key of mymap'),
                      Text('here i want to print value of mymap'),
                    ],)),
              );
              
            });
    
      }
    
    1 回复  |  直到 2 年前
        1
  •  1
  •   Peter Koltai    2 年前

    试试这个:

    Widget build(BuildContext context) {
      Map<String, double> mymap = {
        'Provision': 6300,
        'Food': 3230,
        'shopping': 5039,
        'petrol': 1323
      };
      return ListView(
          children: mymap.keys
              .map((key) => Card(
                    child: Container(
                        height: 100,
                        child: Column(
                          mainAxisSize: MainAxisSize.min,
                          children: [
                            Text(key),
                            Text(mymap[key].toString()),
                          ],
                        )),
                  ))
              .toList());
    }