102

This might be a very simple question but I am having trouble finding an answer. I have a object/map that I would not like to iterate but access a specific key/value at an index.

For example:

var _results = {
  'Key_1' : 'Value_1',
  'Key_2' : 'Value_2', 
};

How would I access the index[1]'s key_2 and value_2?

I've tried _results[index], _results[index].value, _results[index].key and _results[index].toString() but all returning null.

Jesse
  • 2,690
  • 9
  • 15
  • 28

7 Answers7

170

A map allows looking up values by keys. So:

print(_results["Key_1"]);  // prints "Value_1"

A map is not a list of pairs, you cannot access by index.

You can access the keys, values and entries (pairs of key and value) as Iterables.

for (var key in _results.keys) print(key); // prints Key_1 then Key_2
for (var value in _results.values) print(value); // prints Value_1 then Value_2
for (var entry in _results.entries) {
  print(entry.key);
  print(entry.value);
}

You can even convert the entries to a list if you really want to index by integer:

var entryList = _results.entries.toList();
print(entryList[0].key);  // prints "Key_1"

Still, this is not what maps are designed and optimized for. The map literal you wrote will iterate the entries, and their keys and values, in the order they occur in the source, but not all maps guarantee that. If you actually need a list of pairs of strings, I'd recommend making your data that from the beginning, instead of using a map. It will very likely be more efficient, and possibly easier to read.

lrn
  • 64,680
  • 7
  • 105
  • 121
87

convert values ​​to List:

var _list = _results.values.toList();

get by index:

print(_list[1]);
EdHuamani
  • 1,857
  • 13
  • 17
  • 1
    This works thanks! However, are there no explicit way to access without changing it to a list? This is because I'm trying to access the Object's item inside a `listViewItemBuilder` which is already iterating the object[] with index. – Jesse Dec 18 '18 at 00:42
  • 8
    Try `_results.values.first` or `_results.values.elementAt(0)`. It's not necessarily fast for positions above zero (for `_results.values.elementAt(n)` it's likely just going to iterate until it has seen `n` values). – lrn Dec 19 '18 at 07:16
39

To print a value of a key, just use:

void main(){

  var results = {
  'usd' : 3.80,
  'eur' : 4.10, 
  'gbp' : 4.90, 
  };

  print(results['usd']); //3.80
}
Fellipe Sanches
  • 7,395
  • 4
  • 32
  • 33
4

A map is by nature "unordered", except by accident of some implementations. So there's not a "first" key/value pair, so your original question doesn't make sense.

Randal Schwartz
  • 39,428
  • 4
  • 43
  • 70
1

This is what i do to access object inside list in flutter :

getOrderType() async{
    setState(() {
      order.add(new OrderTransactionTypeModel("1", "table", "TABLE"));
      order.add(new OrderTransactionTypeModel("10", "freeTable", "FREE TABLE"));
      order.add(new OrderTransactionTypeModel("7", "jasa", "JASA"));
      order.add(new OrderTransactionTypeModel("2", "bungkus", "BUNGKUS"));
      order.add(new OrderTransactionTypeModel("4", "delivery", "DELIVERY"));
      order.add(new OrderTransactionTypeModel("6", "ojekOnline", "OJEK ONLINE"));
      order.add(new OrderTransactionTypeModel("11", "reservasi", "RESERVASI"));
      order.add(new OrderTransactionTypeModel("99", "lainnya", "LAINNYA"));

      var mappedOrder = order.map((n) => 'Id: ${n.orderId} name: ${n.orderName}');
    });

    print(mappedOrder.toString());
  }
Cevin Ways
  • 984
  • 11
  • 13
1
 void main() {
      print(map.keys);
      print(map.values);
    
      //Print map value
      print(map["1"]);
    
      //Print keys
      for(var i in map.keys){
        print(i);
      }
      //Print value
      for(var i in map.values){
        print(i);
      }
    
      //print keys
      map.keys.forEach((element) { 
        print(element);
      });
    
      // print value
      map.values.forEach((element) {
        print(element);
      });
      
    }
    
    Map<String, dynamic> map = {
      "1": "A",
      "2": "B",
      "3" : "C",
      "4" : "D"
    };
Jamirul islam
  • 502
  • 6
  • 10
0
Map map = {"id": 1, "email": "javedmughal609@gmail.com"};

If you have data in map like above and you want to show all keys of map then you've to do this:

print(map.keys);

and if you want to print only values of inside map then you've to do this:

print(map.values);

If you want to convert your map into list, one list contains only keys and other list contains only values then you have to do these steps:

var onlyKey = map.entries.toList();

and to print list index_vise print(onlyKey[0].key);

var onlyValues = map.values.toList();

and to print list index_vise print(onlyValues[0]);