Home > Enterprise >  how to user key and value of a map in list view builder in flutter
how to user key and value of a map in list view builder in flutter

Time:10-02

I have created a demo for understanding Map

here I have a Map<String,double> and want to make a list view where a card shows key and value

here is my code for making it clear

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

  }

CodePudding user response:

Try this:

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());
}
  • Related