Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

how to get key from map in flutter at required position's key in flutter

Its not easy for me to explain what I want and thats why I have created this demo to make my point clear…

I have a data mymap which key is based on random number and value is bool

in my list tile of listview builder, I want sometime value and sometime key in it’s
title,subtitle,trailing,leading

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

class HomeScreen extends StatelessWidget {



  Map<String,dynamic> mymap={
    '${Random().nextInt(255)}':false,
    '${Random().nextInt(255)}':true,
    '${Random().nextInt(255)}':true,
    '${Random().nextInt(255)}':false,
   

  };

@override
  Widget build(BuildContext context) {
    return ListView.builder(
        itemCount: 1,
        itemBuilder: (context,index){

          return ListTile(
            title: Text(
                //firstkey + value
            ),
            subtitle: Text(
              //secondkey +value
            ),

            leading: Text(
              //thirdkey only
            ),
            trailing: Text(
              //fourthkey's value
            ),
          );

        });
  }
}


>Solution :

You can get keys like here:

List keys = myMap.keys.toList();

And get access to keys in list tile like here:

Text(keys[2].toString()), // get key from list index
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading