Home > Back-end >  how to get key from map in flutter at required position's key in flutter
how to get key from map in flutter at required position's key in flutter

Time:08-31

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

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
            ),
          );

        });
  }
}


CodePudding user response:

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

CodePudding user response:

You can use List to make sure of getting sorted value.

class HomeScreen extends StatelessWidget {
  List<Map<String, bool>> mymap = [
    {'${Random().nextInt(255)}': false},
    {'${Random().nextInt(255)}': true},
    {'${Random().nextInt(255)}': true},
    {'${Random().nextInt(255)}': false},
  ];

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: ListView.builder(
          itemCount: 1,
          itemBuilder: (context, index) {
            return ListTile(
              title:
                  Text("${mymap.first.keys.first} ${mymap.first.values.first}"),
              subtitle: Text("${mymap[1].keys.first} ${mymap[1].values.first}"),
              leading: Text("${mymap[2].keys.first}"),
              trailing: Text("${mymap[3].values.first}"),
            );
          }),
    );
  }
}

Perhaps A model class List is better.

CodePudding user response:

Convert you map to List and us that:

  List<Map<String, dynamic>> list = [];
  mymap.forEach((k,v) => list.add({k:v}));

and use it like this:

ListTile(
              title:
                  Text("${list[0].keys.first} ${list[0].values.first}"),
              subtitle: Text("${list[1].keys.first} ${list[1].values.first}"),
              leading: Text("${list[2].keys.first} ${list[2].values.first}"),
              trailing: Text("${list[3].keys.first} ${list[3].values.first}"),
            );

with this, you convert this:

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

to this:

 Lsit<Map<String,dynamic>> list=[{
    '${Random().nextInt(255)}':false},
    {'${Random().nextInt(255)}':true},
    {'${Random().nextInt(255)}':true},
    {'${Random().nextInt(255)}':false},
  }];
  • Related